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

Syntax of structs

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



Joined: 12 Oct 2020
Posts: 48

View user's profile Send private message

Syntax of structs
PostPosted: Thu Oct 23, 2025 6:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 23, 2025 10:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 23, 2025 12:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 29, 2025 8:45 pm     Reply with quote

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". Smile Razz
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19967

View user's profile Send private message

PostPosted: Wed Oct 29, 2025 11:51 pm     Reply with quote

Yes. Also the using ALL_CAPITALS for macros.
Little 'standards' that make interpretation much easier.
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