Jump to content
Sign in to follow this  
Justin

Nameless Embedded Structures

Recommended Posts

Nameless embedded structures -

A nice feature supported by Visual C++ - see excerpt below from Visual C++ reference:

 

Structure declarations can also be specified without a declarator when they are members of another structure or union. The field names are promoted into the enclosing structure. For example, a nameless structure looks like this:

struct s
{
float y;
struct
{
	int a, b, c;
};
char str[10];
} *p_s;
.
.
.
p_s->b = 100;  /* A reference to a field in the s structure */

 

Justin

Share this post


Link to post
Share on other sites

What are the advantages of nameless embedded structures vs the conventional way?

 

struct s
{
float y;
int a, b, c;
char str[10];
} *p_s;
.
.
.
p_s->b = 100;  /* A reference to a field in the s structure */

 

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Hi Pavel,

 

I think it's most useful with structs embedded within a union, in my case specifically to allow easy EEPROM read/write.

 

Not sure how often this would arise, but consider the following hypothetical situation (my actual situation is similar but has more levels embedded):

 

typedef union tagOBJECT
{
struct
{
	char Data1;
	char Data2;
} Data;			//<<==Undesirable declaration
char AsChar[2];
} OBJECT;

typedef union tagPAIR
{
struct
{
	OBJECT Left;
	OBJECT Right;
} Objects;		//<<==Undesirable declaration
char AsChar[4];
} PAIR;

PAIR MyPair;
MyPair.Objects.Left.Data.Data1 = 0x01; //Allowed

MyPair.Left.Data1 = 0x01;	//Not allowed

Regards

Justin

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×