typedef query

Discussion in 'C Programming' started by James Brown, Oct 23, 2005.

  1. James Brown

    James Brown Guest

    I can't find this covered in the FAQ:

    typedef int foo;

    typedef struct foo
    {
    int a, b, c;

    } bar;

    int main()
    {
    struct foo f1;
    foo f2;
    return 0;
    }

    it compiles with just a warning about unused variables f1, f2

    but this seems ambiguous to me. What type is "foo" - is it
    an "int", or a "struct" ?

    how should a compiler treat this type "foo"??

    thanks,
    James
    James Brown, Oct 23, 2005
    #1
    1. Advertising

  2. James Brown wrote:
    > I can't find this covered in the FAQ:
    >
    > typedef int foo;
    >
    > typedef struct foo
    > {
    > int a, b, c;
    >
    > } bar;
    >
    > int main()
    > {
    > struct foo f1;
    > foo f2;
    > return 0;
    > }
    >
    > it compiles with just a warning about unused variables f1, f2
    >
    > but this seems ambiguous to me. What type is "foo" - is it
    > an "int", or a "struct" ?
    >
    > how should a compiler treat this type "foo"??
    >
    > thanks,
    > James
    >
    >

    "foo" is an int
    "struct foo" is a struct

    Tags of structures, unions and enumerations belong to one namespace,
    ordinary identifiers to another. See paragraph 6.2.3 of the 'C' standard.

    Robert
    Robert Harris, Oct 23, 2005
    #2
    1. Advertising

  3. James Brown

    Joe Wright Guest

    James Brown wrote:
    > I can't find this covered in the FAQ:
    >
    > typedef int foo;
    >
    > typedef struct foo
    > {
    > int a, b, c;
    >
    > } bar;
    >
    > int main()
    > {
    > struct foo f1;
    > foo f2;
    > return 0;
    > }
    >
    > it compiles with just a warning about unused variables f1, f2
    >
    > but this seems ambiguous to me. What type is "foo" - is it
    > an "int", or a "struct" ?
    >

    Given..

    typedef int foo;

    foo is now an synonym for int.

    Given struct foo, foo is a tag and does not conflict with the typedef of
    the same name.

    But struct foo is synonymous with bar. Regard..

    #include <stdio.h>

    typedef int foo;

    typedef struct foo {
    int a, b, c;
    } bar;

    int main(void) {
    bar f0;
    struct foo f1;
    foo f2;
    f2 = 42;
    f0.a = f2;
    f1.a = f2;
    printf("%d %d %d\n", f2, f0.a, f1.a);
    return 0;
    }

    > how should a compiler treat this type "foo"??
    >
    > thanks,
    > James
    >
    >



    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Oct 23, 2005
    #3
  4. "James Brown" <dont_bother> writes:
    > I can't find this covered in the FAQ:
    >
    > typedef int foo;
    >
    > typedef struct foo
    > {
    > int a, b, c;
    >
    > } bar;
    >
    > int main()
    > {
    > struct foo f1;
    > foo f2;
    > return 0;
    > }
    >
    > it compiles with just a warning about unused variables f1, f2
    >
    > but this seems ambiguous to me. What type is "foo" - is it
    > an "int", or a "struct" ?
    >
    > how should a compiler treat this type "foo"??


    As others have pointed out, there is no ambiguity.

    <OT>
    You may be confusing this with C++. In C++, the "struct foo"
    declaration allows you to refer to the type as either "struct foo" or
    "foo". This may create an ambiguity; comp.lang.c++ (or a C++
    textbook, or the C++ standard, or your C++ compiler if it's
    conforming) would be the place to ask how it's resolved.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 23, 2005
    #4
  5. James Brown

    James Brown Guest

    "James Brown" <dont_bother> wrote in message
    news:...
    >I can't find this covered in the FAQ:
    >
    > typedef int foo;
    >
    > typedef struct foo
    > {
    > int a, b, c;
    >
    > } bar;
    >
    > int main()
    > {
    > struct foo f1;
    > foo f2;
    > return 0;
    > }
    >
    > it compiles with just a warning about unused variables f1, f2
    >
    > but this seems ambiguous to me. What type is "foo" - is it
    > an "int", or a "struct" ?
    >
    > how should a compiler treat this type "foo"??
    >
    > thanks,
    > James
    >


    thanks for the replies, I hadn't appreciated that there was a
    separate namespace for structure-tags

    James
    James Brown, Oct 24, 2005
    #5
    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. Learner
    Replies:
    1
    Views:
    976
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,445
    Anonymous
    Oct 13, 2005
  3. Neil McPhail

    A typedef struct query

    Neil McPhail, Jul 24, 2004, in forum: C Programming
    Replies:
    2
    Views:
    369
    Emmanuel Delahaye
    Jul 24, 2004
  4. tropos
    Replies:
    2
    Views:
    297
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Sep 17, 2007
  5. oor
    Replies:
    0
    Views:
    1,336
Loading...

Share This Page