typedef struct

J

John McDermick

Hello,

If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What does it mean when I declare like this:

typedef struct A
{
int test;
};

And what does it mean when I declare like this:

typedef struct
{
int test;
} B;
 
E

Eric Sosman

Hello,

If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

`A' is a "struct tag," something that distinguishes one particular
kind of struct from other kinds. The combination `struct A' names the
type of that particular kind of struct, so you can use that name in
other contexts like `struct A instance;' to declare a variable of that
struct type or `struct A *pointer;' to declare a variable that points
to objects of that type. `A' by itself means nothing in C; the rules
in C++ are different.

`B' is an alias for the type `struct A', and can be used wherever
`struct A' could. So `B instance;' declares a variable of the struct
type, and `B *pointer;' declares a variable that can point to it. `B'
and `struct A' are interchangeable.
What does it mean when I declare like this:

typedef struct A
{
int test;
};

It means you declare `struct A' and `A' with the meanings as above.
Then you start to declare an alias for `struct A' but never provide the
alias' name. I'm not sure the Standard requires a diagnostic for this
vacuous attempt at an alias, but compilers are likely to warn about it
in any case. It's a lot like writing `typedef float;' and never giving
the name that you want to use as an alias for `float'.
And what does it mean when I declare like this:

typedef struct
{
int test;
} B;

Again you declare a struct type, but this one has no name of its
own: There is no way to complete `struct ???' to name the type of
this struct. However, you also declare `B' as an alias for that
anonymous struct type, so you can still write `B instance;' and
`B *pointer;'. So, `B' is an alias for a type that has no true name
of its own, but `B' will serve as a perfectly good substitute.
 
B

BartC

John McDermick said:
Hello,

If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

It gets more confusing when you write it like this:

typedef struct A
{
int test;
} A;

Both A and A can co-exist, because the first A (A in your example) needs to
be used as:

struct A x;

while the second A (B in your example) can be written like this:

A x;
What does it mean when I declare like this:

typedef struct A
{
int test;
};

This one puzzles me too; adding the 'typedef' doesn't buy you much more than
just writing struct A{int test;}. In fact, gcc will complain about it so it
agrees with me!
And what does it mean when I declare like this:

typedef struct
{
int test;
} B;

This declares a type alias for struct{int test;}, called B.
 
K

Keith Thompson

John McDermick said:
If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What does it mean when I declare like this:

typedef struct A
{
int test;
};

And what does it mean when I declare like this:

typedef struct
{
int test;
} B;

Eric has ably answered your question.

Opinions differ on this, but personally I don't feel the need for a
typedef at all. I'd just declare the type as

struct A {
int test;
};

and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

You can also use the same identifier for both the tag and the typedef:

typedef struct A {
int test;
} A;

You can then refer to the type either as "struct A" or as "A". Since
struct tags are in a different namespace than typedef names (since the
tag can only appear immediately after the keyword "struct', and the
typedef name never can), there's no conflict.

(Note that C++ makes the struct tag usable directly as a type name; even
without the typedef, you can refer to the type as just "A". C doesn't
do this.)
 
K

Kulin

What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?
struct A {
struct A *p;
};

and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.
 
E

Eric Sosman

What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?


My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration
struct A { // "completes" the type `struct A'
int test;
B *p1;
};
 
X

Xavier Roche

Le 31/05/2012 21:10, Kulin a écrit :
What if I want to create a pointer to the struct inside the struct?
typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

Hum, why don't you first define the type incompletely ?

typedef struct A A;
struct A {
A *p1;
A *p2;
};
 
K

Keith Thompson

Kulin said:
What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer? [snip]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

Why do you want to avoid using the "struct" keyword?

But if you want to use a typedef, there's no real need to use different
identifiers for the struct tag and the typedef name. This:

typedef struct A A;

is perfectly valid and doesn't create any potential ambiguity. Or at
least use a consistent convention, like:

typedef struct A_ A;
 
N

Nomen Nescio

Keith Thompson said:
Kulin said:
What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer? [snip]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that.
But if you want to use a typedef, there's no real need to use different
identifiers for the struct tag and the typedef name. This:

typedef struct A A;

is perfectly valid and doesn't create any potential ambiguity. Or at
least use a consistent convention, like:

typedef struct A_ A;

Thanks a lot!

Carl
 
K

Keith Thompson

Nomen Nescio said:
Keith Thompson said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?
 
I

Ian Collins

Nomen Nescio said:
Keith Thompson said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

He or she certainly does on other groups and mail lists.
 
I

Ike Naar

Nomen Nescio said:
Keith Thompson said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

Remember "George Orwell", "Borked Pseudo Mailed", "Han from China" et al. ?
 
B

BartC

Keith Thompson said:
Nomen Nescio said:
Keith Thompson said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

"Nomen Nescio" said on another group that he/she uses 'anonymous remailers'.
But with no warning that they will be using different names, and no
signature on the posts either, things get confusing.
 
F

Fritz Wuehler

Keith Thompson said:
Nomen Nescio said:
Keith Thompson said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

No, I'm posting as myself but it gets posted through various remailers!

Thanks for the help!

Carl
 
K

Keith Thompson

Fritz Wuehler said:
Keith Thompson said:
Nomen Nescio said:
Why do you want to avoid using the "struct" keyword?

If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that. [...]
Carl

My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

No, I'm posting as myself but it gets posted through various remailers!

Thanks for the help!

Would you consider adding some consistent header line to all your posts
so we can tell it's you?
 
Õ

ÕÅÔ´ Î÷±±¹¤Òµ´óѧ

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?
struct A { // "completes" the type `struct A'
int test;
B *p1;
};


typedef struct A B; // "incomplete type" declaration
struct A { // "completes" the type `struct A'
int test;
B *p1;
};
 
Õ

ÕÅÔ´ Î÷±±¹¤Òµ´óѧ

Le 31/05/2012 21:10, Kulin a écrit :

Hum, why don't you first define the type incompletely ?

typedef struct A A;
struct A {
A *p1;
A *p2;
};

can you explain why we can typedef incompletely?
such as some basic implementation on computer for this incompletely typedef?

(sorry for my awful English)
 
E

Eric Sosman

[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top