View previous topic :: View next topic |
Author |
Message |
c_user
Joined: 19 May 2010 Posts: 9
|
Initialize integer array in structure |
Posted: Wed May 19, 2010 4:04 pm |
|
|
I have a simple struct as follows:
Code: | typedef struct
{
int8 val[8];
int8 type;
} init_struct;
init_struct init_example; |
Then I try to initialize array, val, as follows:
Code: | init_example.val = {1,2,3,4,5,6,7};
init_example.type = 6; |
When I compile, I get the following error messages for init_example.val line:
Quote: | A numeric expression must appear here
Expect ; |
Can anyone tell me what I am doing wrong? |
|
|
c_user
Joined: 19 May 2010 Posts: 9
|
|
Posted: Wed May 19, 2010 4:19 pm |
|
|
I am using version 4.109. Also the following (with brackets) gives the same error:
Code: | init_example.val[] = {1,2,3,4,5,6,7};
init_example.type = 6; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 19, 2010 4:27 pm |
|
|
The versions page says the lastest version is 4.107:
http://www.ccsinfo.com/devices.php?page=versioninfo
If you want to initialize the structure, you can do it in the same statement
where you declare the structure. Example:
Code: |
typedef struct
{
int8 val[8];
int8 type;
}init_struct;
init_struct init_example = {{1,2,3,4,5,6,7,8}, 9};
|
This fills the array and the 'type' variable. |
|
|
c_user
Joined: 19 May 2010 Posts: 9
|
|
Posted: Wed May 19, 2010 4:33 pm |
|
|
My mistake - I am using 4.107
There is a reason I need to initialize the array, in the way I am doing it. This is extracted code to present the problem.
Am I violating a syntactic rule of C? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 19, 2010 4:50 pm |
|
|
You can't use the comma-separated syntax to load an array at run-time. |
|
|
c_user
Joined: 19 May 2010 Posts: 9
|
|
Posted: Wed May 19, 2010 6:48 pm |
|
|
Thanks for identifying the problem. |
|
|
|