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 CCS Technical Support

Defining/initialize an array of structs

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



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

Defining/initialize an array of structs
PostPosted: Thu Jun 06, 2019 12:28 pm     Reply with quote

I can't seem to find a way to define an array of structs using CCS v5.081

This code compiles:

Code:

typedef struct
{
    uint8_t     closedReads;
    uint8_t     threshold;
    int1        dbState;
   
} swPos_t;

swPos_t     switch1     =           {0, 25, 0};


This code compiles too:

Code:

typedef struct
{
    uint8_t     closedReads;
    uint8_t     threshold;
    int1        dbState;
   
} swPos_t;

swPos_t     switches[9];


But when I actually try to initialize the array subelement struct, CCS kicks back a compiler error "Expecting a (", "Expecting a declaration":

Code:

// THIS CODE DOES NOT COMPILE //
typedef struct
{
    uint8_t     closedReads;
    uint8_t     threshold;
    int1        dbState;
   
} swPos_t;

swPos_t     switches[9];

switches[0]     =       {0, 25,  0};


Also if I neglect the typedef and just do struct, this also does not compile:


Code:

// THIS CODE DOES NOT COMPILE //
struct swPos
{
    uint8_t     closedReads;
    uint8_t     threshold;
    int1        dbState;
   
};

struct swPos     switches[9];

switches[0]     =       {0, 25,  0};



Why does the above code not compile?
Is there another, non-standard C way to get this to compile?
As far as I can tell what I've written above is standard C, so why does this not compile with CCS?
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Thu Jun 06, 2019 12:44 pm     Reply with quote

What you post, is supported, but only for initialisation. So at the time
of declaration, not 'in code'. To set multiple elements 'in code', you would
have to write your own function to do this. Quote from another board
(not CCS, but a more generic C), about this:
Quote:

It is a special initialization syntax, and you can't do something similar after initialization of your struct. What you can do is provide a member (or non-member) function to take your series of values as parameters which you then assign within the member function - that would allow you to accomplish this after the structure is initialized in a way that is equally concise (after you've written the function the first time of course!)


So this is not a CCS limitation, but a limitation in C.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 06, 2019 1:01 pm     Reply with quote

I was sort of wondering something similar too... I had just started to think about this yesterday, coincidentally. But then I realized that in my application it did not matter whether I initialized with default values or not so I moved on. I am interested in this answer for the future though.
apakSeO



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

PostPosted: Thu Jun 06, 2019 1:07 pm     Reply with quote

Ttelmah wrote:
What you post, is supported, but only for initialisation. So at the time
of declaration, not 'in code'. To set multiple elements 'in code', you would
have to write your own function to do this. Quote from another board
(not CCS, but a more generic C), about this:
Quote:

It is a special initialization syntax, and you can't do something similar after initialization of your struct. What you can do is provide a member (or non-member) function to take your series of values as parameters which you then assign within the member function - that would allow you to accomplish this after the structure is initialized in a way that is equally concise (after you've written the function the first time of course!)


So this is not a CCS limitation, but a limitation in C.


OK, I was able to get compilation doing the following:


Code:

struct swPos
{
    uint8_t     closedReads;
    uint8_t     threshold;
    int1        dbState;
   
};

struct swPos    switches[2] = {
                                {0, 25, 0},
                                {0, 25, 0}
                            };


Originally I had used "switch[2]" which caused a compiler error because "switch" is a reserved syntax keyword. Oopsie.

Thanks again!
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Thu Jun 06, 2019 10:41 pm     Reply with quote

Whoops....

Glad you got it going. Very Happy
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