| View previous topic :: View next topic |
| Author |
Message |
Backfire
Joined: 12 Oct 2020 Posts: 48
|
| Syntax of structs |
Posted: Thu Oct 23, 2025 6:05 am |
|
|
Hi all,
I'm hoping someone can shed some light on an issue I've been facing. I am currently working on a library that I am porting over to use the CCS compiler, and a large amount of the library is implemented as structs of data.
I would like to typedef them to improve readability; but when it comes to compiling I see failure at compilation time if my code is as shown in Example #2. However I was under the impression that both Example #1 and Example #2 are valid 'C'...?
Thanks in advance for any help!
| Code: |
//Example #1 - Compiles without issue
typedef struct
{
int8 Var1;
int8 ByteArray[];
}ExampleStruct;
ExampleStruct MyStruct = {0x01, 0x00};
|
| Code: |
//Example #2 - Fails compilation with Error "Expecting a ("
typedef struct ExampleStruct
{
int8 Var1;
int8 ByteArray[];
};
ExampleStruct MyStruct = {0x01, 0x00};
|
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19967
|
|
Posted: Thu Oct 23, 2025 10:07 am |
|
|
The former is the correct syntax. the later generates an anonymous structure type, which some compilers will accept, but is not how K&R
suggests this is done.
The really full syntax, would be to give the structure a name, and then
give the type a name as well. So (for example)
| Code: |
typedef structure datanode {
unsigned int16 size;
unsigned int16 next_prtr;
} datanode_t;
//then access as
datanode_t node;
|
Which makes the type nice and obviously a type (_t), and also gives
the structure itself a name. |
|
 |
Backfire
Joined: 12 Oct 2020 Posts: 48
|
|
Posted: Thu Oct 23, 2025 12:02 pm |
|
|
Hi Ttelmah,
and thanks for your reply.
I'm a little confused though, my Example #1 is surely the "anonymous structure type"?
I thought my Example #2, was defined in a more 'C standard compliant' format, yet it is Example #2 that fails.
This just seemed like a compiler quirk. Is this behaviour somehow being caused by the 'Flexible Array Member (ByteArray)' I am using?
I will be using your recommened full syntax style at any rate moving forwards, thanks again. |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1617 Location: Central Illinois, USA
|
|
Posted: Wed Oct 29, 2025 8:45 pm |
|
|
| Ttelmah wrote: | The former is the correct syntax. the later generates an anonymous structure type, which some compilers will accept, but is not how K&R
suggests this is done.
The really full syntax, would be to give the structure a name, and then
give the type a name as well. So (for example)
| Code: |
typedef structure datanode {
unsigned int16 size;
unsigned int16 next_prtr;
} datanode_t;
//then access as
datanode_t node;
|
Which makes the type nice and obviously a type (_t), and also gives
the structure itself a name. |
And it's worth mentioning that the convention using "_t" is (to my travels) how all the Unix/Linux pros/veterans typedef defines stuff.
So "get used to it now. you're going to see it a lot".  _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19967
|
|
Posted: Wed Oct 29, 2025 11:51 pm |
|
|
Yes. Also the using ALL_CAPITALS for macros.
Little 'standards' that make interpretation much easier. |
|
 |
|