|
|
View previous topic :: View next topic |
Author |
Message |
apakSeO
Joined: 07 Dec 2016 Posts: 60 Location: Northeast USA
|
Defining/initialize an array of structs |
Posted: Thu Jun 06, 2019 12:28 pm |
|
|
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: 19535
|
|
Posted: Thu Jun 06, 2019 12:44 pm |
|
|
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
|
|
Posted: Thu Jun 06, 2019 1:01 pm |
|
|
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
|
|
Posted: Thu Jun 06, 2019 1:07 pm |
|
|
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: 19535
|
|
Posted: Thu Jun 06, 2019 10:41 pm |
|
|
Whoops....
Glad you got it going. |
|
|
|
|
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
|