typedef query

J

James Brown

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
 
R

Robert Harris

James said:
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
 
J

Joe Wright

James said:
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;
}
 
K

Keith Thompson

James Brown said:
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>
 
J

James Brown

James Brown said:
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
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top