473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

typedef query

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
Nov 15 '05 #1
4 1473
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
Nov 15 '05 #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" ?
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 ---
Nov 15 '05 #3
"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) ks***@mib.org <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.
Nov 15 '05 #4
"James Brown" <dont_bother> wrote in message
news:vv********************@pipex.net...
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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
7
by: ThazKool | last post by:
Is there an elegant/efficient way to use a typedef of a class passed in to a template parameter. I know that a second template parameter can be given, but I was wondering if there is any way to...
6
by: Lorn | last post by:
I was hoping some of you might be able to help me understand the following code defining a typedef for a time query to the WINAPI. I understand the basics of what's going on, I just don't...
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
2
by: tropos | last post by:
I'm trying to use the 'typedef templates' idiom, mentioned in Alexandrescu's work on templates. Here's a Dr Dobbs article from 2002 that says they are a proposed C++ standard:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.