typedef struct

Discussion in 'C Programming' started by John McDermick, Oct 15, 2011.

  1. 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;
     
    John McDermick, Oct 15, 2011
    #1
    1. Advertising

  2. John McDermick

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Oct 15, 2011
    #2
    1. Advertising

  3. John McDermick

    BartC Guest

    "John McDermick" <> wrote in message
    news:...
    > 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
     
    BartC, Oct 15, 2011
    #3
  4. John McDermick <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 16, 2011
    #4
  5. Awesome and easy to understand explanations!

    Thank you to all of you :eek:)
     
    John McDermick, Oct 16, 2011
    #5
  6. John McDermick

    Kulin Guest

    Keith Thompson <> wrote:

    > John McDermick <> 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.
     
    Kulin, May 31, 2012
    #6
  7. John McDermick

    Eric Sosman Guest

    On 5/31/2012 3:10 PM, Kulin wrote:
    > Keith Thompson<> wrote:
    >
    >> John McDermick<> 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
    d
     
    Eric Sosman, May 31, 2012
    #7
  8. John McDermick

    Xavier Roche Guest

    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;
    };
     
    Xavier Roche, May 31, 2012
    #8
  9. Kulin <> 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) <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"
     
    Keith Thompson, May 31, 2012
    #9
  10. John McDermick

    Nomen Nescio Guest

    Thank you very much!

    Carl
     
    Nomen Nescio, Jun 1, 2012
    #10
  11. John McDermick

    Nomen Nescio Guest

    Keith Thompson <> wrote:

    > Kulin <> 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?


    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
     
    Nomen Nescio, Jun 1, 2012
    #11
  12. Nomen Nescio <> writes:
    > Keith Thompson <> wrote:
    >> Kulin <> writes:

    [...]
    >> 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?

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Jun 1, 2012
    #12
  13. John McDermick

    Ian Collins Guest

    On 06/ 2/12 06:45 AM, Keith Thompson wrote:
    > Nomen Nescio<> writes:
    >> Keith Thompson<> wrote:
    >>> Kulin<> writes:

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

    --
    Ian Collins
     
    Ian Collins, Jun 1, 2012
    #13
  14. John McDermick

    Ike Naar Guest

    On 2012-06-01, Keith Thompson <> wrote:
    > Nomen Nescio <> writes:
    >> Keith Thompson <> wrote:
    >>> Kulin <> writes:

    > [...]
    >>> 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. ?
     
    Ike Naar, Jun 1, 2012
    #14
  15. John McDermick

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Nomen Nescio <> writes:
    >> Keith Thompson <> wrote:
    >>> Kulin <> writes:

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

    --
    Bartc
     
    BartC, Jun 1, 2012
    #15
  16. Keith Thompson <> wrote:

    > Nomen Nescio <> writes:
    > > Keith Thompson <> wrote:
    > >> Kulin <> writes:

    > [...]
    > >> 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
     
    Fritz Wuehler, Jun 2, 2012
    #16
  17. Fritz Wuehler <>
    writes:
    > Keith Thompson <> wrote:
    >> Nomen Nescio <> writes:
    >> > Keith Thompson <> wrote:
    >> >> Kulin <> writes:

    >> [...]
    >> >> 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?

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Jun 3, 2012
    #17
  18. On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
    > On 5/31/2012 3:10 PM, Kulin wrote:
    > > Keith Thompson<> wrote:
    > >
    > >> John McDermick<> 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


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

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




    >
    > --
    > Eric Sosman
    > d



    On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
    > On 5/31/2012 3:10 PM, Kulin wrote:
    > > Keith Thompson<> wrote:
    > >
    > >> John McDermick<> 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
    > d




    On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
    > On 5/31/2012 3:10 PM, Kulin wrote:
    > > Keith Thompson<> wrote:
    > >
    > >> John McDermick<> 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
    > d
     
    ÕÅÔ´ Î÷±±¹¤Òµ´óѧ, Jun 8, 2012
    #18
  19. On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
    > 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;
    > };


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

    (sorry for my awful English)
     
    ÕÅÔ´ Î÷±±¹¤Òµ´óѧ, Jun 8, 2012
    #19
  20. John McDermick

    Eric Sosman Guest

    On 6/8/2012 1:02 PM, å¼ æº è¥¿åŒ—å·¥ä¸šå¤§å­¦ wrote:
    > On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
    >> On 5/31/2012 3:10 PM, Kulin wrote:
    >>> [...]
    >>> 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).

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


    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.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jun 8, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    516
  2. DanielEKFA
    Replies:
    8
    Views:
    631
    DanielEKFA
    May 16, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    689
  5. oor
    Replies:
    0
    Views:
    1,398
Loading...

Share This Page