[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...difference between typedef struct {...} x; and...

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

Question by kk
Submitted on 6/2/2004
Related FAQ: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)
Rating: Rate this question: Vote
what is the difference between
typedef struct {...} x;
and typedef struct x { ... };




Answer by harish
Submitted on 6/3/2004
Rating:  Rate this answer: Vote
when u say struct { }x; this means that u have an anonymous structure and x is a variable of that structure.

when u use typedef struct x{ } then it has created a new data type called x which is a structure itself.so u can define variables
by using

x variable1,variable2  etc.x itself is not a variable but its a tagname.

 

Answer by filifala
Submitted on 11/3/2004
Rating: Not yet rated Rate this answer: Vote
typedef struct {...} x; creates a new data type x containing the structure of {...}

declaring variables from this new data type:

x x1;
x *x1;

meanwhile,
typedef struct x {..}; is rather an oxymoron declaration. it defines a structure named x, but of no type name. therefore it is not possible to declare new variables like above. declaring new var. would be in the form:

struct x x1; or
struct x *x1;

but,

struct {..} x,y,z; creates three vars from the anon. struct.

and,

struct x {...} a,b,c; creates a structure named x and three variables of the same structure (a,b,c).

lastly,

typedef struct x {struct *x member1;...} y;
is a useful way of declaring a type with circular members (it contains members of it's own kind).

 

Answer by BJ
Submitted on 1/2/2006
Rating: Not yet rated Rate this answer: Vote
"typedef struct {...} x;" is clean code. You are declaring a struct containing elements ..., and calling it x.

"typedef struct x { ... };" is weird. Visual C++ will simply ignore the 'typedef' at the beginning of the line because you've left out the typedef label (the "x" at the end of the previous line). It will interpret the line as, "struct x { ... }". (I don't know if other compilers will behave the same way.)

So what's the difference between
typedef struct {...} x;
and
struct x { ... }; ?

Answer: Very little. In both cases you're creating a struct called 'x', but there are a few small differences.

In C, with the second version, you have to declare variables with "struct x myX;". You can do that in C++ too, but you also have the option of writing "x myX;". With the first version, variables are declared with "x myX;" in both C and C++, so the first version provides better consistency between the two languages. If that's important to you, the first version is the better choice.

Also, in C++, you can have constructors for structs, e.g.
struct x
{
   x(int iVal=0) {i = iVal;}
   int i;
}

Now the line, "x myX;" initializes myX.i to 0 and "x MyX(val);" initializes myX.i to val. This only works for the second version though, because the constructor must have the same name as the struct. In the first version, you haven't really named the struct, just typedef'd it, so it can't have a constructor.

You can also legally combine both versions, e.g. "typedef struct x {....} typeX;" The struct could then be referred to as "struct x", "typeX", or "x" (C++ only). It could make the code more confusing though, since coders could use "struct x" in some places and "typeX" in others, and someone would have to read your typedef statement to know they were referring to the same structure.

 

Answer by hemu
Submitted on 1/17/2006
Rating: Not yet rated Rate this answer: Vote
hi

 

Answer by nate
Submitted on 4/21/2006
Rating: Not yet rated Rate this answer: Vote
The previous reply didn't fully answer the question. This code should make it clear how each is used:

struct A {
int x;
};
typedef struct B {
int x;
} C;
typedef struct {
int x;
} D;

int main(int argc, char **argv) {
struct A a;
struct B b;
C c;
D d;
b = c; /* these types are equivalent */
}

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.