How to assignment an array with a define type at run time?

Status
Not open for further replies.

drzsmith

Newbie level 3
Joined
Mar 18, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,307
I have strings data that I load in to array at compile time.


Code C - [expand]
1
2
3
char array1[] =  ("xxxxx1");
char array2[] = ("xxxxx2");
char array3[] = ("xxxxx3");



I would rather load the values at run time(below)
is this possible?



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
#define str1 xxxx1
#define str2 xxxx2
#define str3 xxxx3
 
main(){
 
 char array[10];
 
//wrong. 
 array = str1;
 
}



thans drz
 
Last edited by a moderator:

You can do some pointer array hacking; e.g.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
char *str="1234567890";
 
main()
{
  char *ptr;
  char y;
...
...
  y = ptr[2];
}



Might be dependent on the compiler and it's optimizations. Also, poorly optimized code would ruin the day - about 20 time slower in some case. Carefully consider reading the assembly code (provided it compiles ).
 
Last edited by a moderator:


Can you explain little more what you actually want to do?
 

I would rather load the values at run time(below) is this possible?



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
#define str1 xxxx1
#define str2 xxxx2
#define str3 xxxx3
 
main(){
 
 char array[10];
 
//wrong. 
 array = str1;
 
}



thans drz

"array" being a char array name, it holds the starting address of the array in the memory. If you try to assign a new address to this pointer during run time (try to destroy the array's existance), there will be compiler error. L-value required will be shown by the compiler
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…