Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > typedef struct

Reply
Thread Tools

typedef struct

 
 
John McDermick
Guest
Posts: n/a
 
      10-15-2011
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;

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-15-2011
On 10/15/2011 2:38 PM, John McDermick wrote:
> 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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      10-15-2011


"John McDermick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

--
Bartc

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-15-2011
John McDermick <(E-Mail Removed)> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
John McDermick
Guest
Posts: n/a
 
      10-16-2011
Awesome and easy to understand explanations!

Thank you to all of you )
 
Reply With Quote
 
Kulin
Guest
Posts: n/a
 
      05-31-2012
Keith Thompson <(E-Mail Removed)> wrote:

> John McDermick <(E-Mail Removed)> writes:
> > If I have this definition in a header file:
> >
> > typedef struct A
> > {
> > int test;
> > } B;
> >
> > What exactly is A and what is B ?


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.



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-31-2012
On 5/31/2012 3:10 PM, Kulin wrote:
> Keith Thompson<(E-Mail Removed)> wrote:
>
>> John McDermick<(E-Mail Removed)> writes:
>>> If I have this definition in a header file:
>>>
>>> typedef struct A
>>> {
>>> int test;
>>> } B;
>>>
>>> What exactly is A and what is B ?

>
> 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.


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

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      05-31-2012
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;
};
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-31-2012
Kulin <(E-Mail Removed)> writes:
[...]
> 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;

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nomen Nescio
Guest
Posts: n/a
 
      06-01-2012
Thank you very much!

Carl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
typedef struct {} SName; vs. struct SName{}; Steven T. Hatton C++ 2 08-03-2005 09:59 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments