Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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:

I have strings data that I load in to array at compile time.

char array1[] = ("xxxxx1");
char array2[] = ("xxxxx2");
char array3[] = ("xxxxx3");

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


#define str1 xxxx1
#define str2 xxxx2
#define str3 xxxx3

main(){

char array[10];

//wrong.
array = str1;

}

thans drz

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top