CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Initialize integer array in structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
c_user



Joined: 19 May 2010
Posts: 9

View user's profile Send private message

Initialize integer array in structure
PostPosted: Wed May 19, 2010 4:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 19, 2010 4:19 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 19, 2010 4:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 19, 2010 4:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 19, 2010 4:50 pm     Reply with quote

You can't use the comma-separated syntax to load an array at run-time.
c_user



Joined: 19 May 2010
Posts: 9

View user's profile Send private message

PostPosted: Wed May 19, 2010 6:48 pm     Reply with quote

Thanks for identifying the problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group