C++ Structures

Started by
2 comments, last by rip-off 16 years, 2 months ago
Hiya, I have a quick question I was hoping someone could answer - what's the difference between these two structure declarations?

struct MYSTRUCT
{
   int a;
};

struct
{
   int a;
} MYSTRUCT;
Thanks for any help - I've always assumed they are the same, but thought I'd check! [smile]
Advertisement
The first defines a structure named MYSTRUCT.

The second defines a variable named MYSTRUCT of an unnamed structure type.
That's great, thanks.
Often in C you will see code like this:
typedef struct Foo {   int a;} Foo;


This doesn't declare any variables, it just allows one to reference a structure without dealing with the structure "namespace".

Example:
struct One { int a; };void frobnicate( struct One * );// void frobnicate( One * ); causes error

Versus:

typedef struct One { int a; } One;// void frobnicate( struct One * ); could still do thisvoid frobnicate( One * ); // easier to read

This topic is closed to new replies.

Advertisement