Typedef with GCC/G++

J

Jeff Mott

Given the following example code

typedef
struct fubar *
fubar;

struct fubar
{
};

int main(void)
{
return 0;
}

GCC on Windows will compile this fine, but G++ throws an error saying

3: error: conflicting types for 'typedef struct fubar*fubar'
2: error: previous declaration as 'struct fubar'
6: error: conflicting types for 'struct fubar'
3: error: previous declaration as 'typedef struct fubar*fubar'

Now I know I can avoid this error by simply renaming the struct to
something else, but I'd prefer to understand why this is interpreted
differently.
 
M

Mac

Given the following example code

typedef
struct fubar *
fubar;

struct fubar
{
};

int main(void)
{
return 0;
}

GCC on Windows will compile this fine, but G++ throws an error saying

3: error: conflicting types for 'typedef struct fubar*fubar'
2: error: previous declaration as 'struct fubar'
6: error: conflicting types for 'struct fubar'
3: error: previous declaration as 'typedef struct fubar*fubar'

Now I know I can avoid this error by simply renaming the struct to
something else, but I'd prefer to understand why this is interpreted
differently.

Seems like you are knowingly asking a C++ question in a C newsgroup.

Maybe you should try comp.lang.c++ instead of comp.lang.c. It would
probably be a good idea to read their FAQ list and/or charter (if they
have one) first, of course.

--Mac
 
A

Arthur J. O'Dwyer

Given the following example code [snip]
GCC on Windows will compile this fine, but G++ throws an error saying

Different languages, different syntax rules and different semantics.
comp.lang.c++ is down the hall --- ask there, if you really want to know
why C++ treats class names as types even without the 'struct' or 'class'
tag attached. This group discusses C, and your example is perfectly
valid C.

-Arthur
 
K

Keith Thompson

Given the following example code

typedef
struct fubar *
fubar;

struct fubar
{
};

int main(void)
{
return 0;
}

GCC on Windows will compile this fine, but G++ throws an error saying

3: error: conflicting types for 'typedef struct fubar*fubar'
2: error: previous declaration as 'struct fubar'
6: error: conflicting types for 'struct fubar'
3: error: previous declaration as 'typedef struct fubar*fubar'

Now I know I can avoid this error by simply renaming the struct to
something else, but I'd prefer to understand why this is interpreted
differently.

G++ is a C++ compiler. Try comp.lang.c++ (where they'll probably tell
you that, unlike in C, given a declaration "struct fubar { /* ... */}",
the type can be referred to as just "fubar").
 
D

Dan Pop

In said:
Given the following example code

typedef
struct fubar *
fubar;

struct fubar
{
};

int main(void)
{
return 0;
}

GCC on Windows will compile this fine, but G++ throws an error saying

3: error: conflicting types for 'typedef struct fubar*fubar'
2: error: previous declaration as 'struct fubar'
6: error: conflicting types for 'struct fubar'
3: error: previous declaration as 'typedef struct fubar*fubar'

Now I know I can avoid this error by simply renaming the struct to
something else, but I'd prefer to understand why this is interpreted
differently.

Engage your brain. If gcc accepted your code, it is quite likely that it
is correct C code, right? If g++ rejected your code, it is quite likely
that it is incorrect C++ code, right?

Since it doesn't make much sense to ask "why is this correct code C code",
your question actually amounts to "why is this incorrect C++ code?".
But what's the point in asking such a question in a C newsgroup?

Dan
 
E

Eric Schmidt

Given the following example code

typedef
struct fubar *
fubar;

struct fubar
{
};

int main(void)
{
return 0;
}

GCC on Windows will compile this fine, but G++ throws an error saying

Actually, I should point out that this code is not valid C, because
your struct has no members. As for C++, that's off topic here.
 
B

Ben Pfaff

Since when did structs have to have members?

Since C89 at least:

struct-or-union-specifier:
struct-or-union identifieropt { struct-declaration-list }
struct-or-union identifier
struct-or-union:
struct
union
struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration
struct-declaration:
specifier-qualifier-list struct-declarator-list ;
 
G

Guillaume

struct fubar
Since when did structs have to have members?

Well, actually, they don't. But they're not very useful this way.

The following code snippet only yields one "shy" warning with gcc:

-----------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct ohmy
{
};

int main(int argc, char **argv)
{
struct ohmy sFunOne;
char aEvenFunnierOne[0];

memcpy(&sFunOne, aEvenFunnierOne, sizeof(struct ohmy));

return 0;
}
 
C

Chris Torek

[someone wrote]
Well, actually, they don't. But they're not very useful this way.

They do in C; they do not in Gnuck ("GNU C").
The following code snippet only yields one "shy" warning with gcc:
[original code snipped; see previous message in thread if needed]
gcc -c -Wall test4.c
test4.c: In function `main':
test4.c:14: warning: statement with no effect

You have to ask GCC to be a C compiler:

[compiling Gnuck code, rather than C - I get no warning at all!]
% gcc -O -c -Wall t.c
%

[compiling C code]
% gcc -O -c -Wall -ansi -pedantic t.c
t.c:7: warning: struct has no members
t.c: In function `main':
t.c:12: warning: ISO C forbids zero-size array `aEvenFunnierOne'
%

These both come out as "warnings" rather than "errors" because gcc
was able to guess what you meant, and hence compile some code that
probably differs from what you actually wrote; but are you sure
gcc's guess is correct? :)
 
R

Robert Gamble

Well, actually, they don't. But they're not very useful this way.

Well, actually the do.
The following code snippet only yields one "shy" warning with gcc:

-----------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct ohmy
{
};

int main(int argc, char **argv)
{
struct ohmy sFunOne;
char aEvenFunnierOne[0];

memcpy(&sFunOne, aEvenFunnierOne, sizeof(struct ohmy));

return 0;
}
-----------------------------------------------------------------------

gcc -c -Wall test4.c
test4.c: In function `main':
test4.c:14: warning: statement with no effect

gcc -Wall test4.c -ansi -pendatic yields:

test10.c:7: warning: struct has no members
test10.c: In function `main':
test10.c:12: warning: ISO C forbids zero-size array `aEvenFunnierOne'

Compiling in c99 mode yields identical results. It's dangerous to
assume that just becuase your compiler accepted something (while
running it in a non-strictly-comforming mode no less) that the Standard
allows it. Ben provided the spec which prohibits memberless struct
definitions in C89, the same holds true for C99 (Section 6.7.2.1).

Rob Gamble
 
G

Guillaume

Compiling in c99 mode yields identical results. It's dangerous to
assume that just becuase your compiler accepted something (while
running it in a non-strictly-comforming mode no less) that the Standard
allows it.

Of course. This was just given for illustration purposes.

You're right, C99 states that:

- An array type describes a contiguously allocated nonempty set of
objects (...)
- A structure type describes a sequentially allocated nonempty set of
member objects (...)

The key word here is "nonempty". C89 seemed more vague about this to me,
but that may just be me. I may haved missed something; I unfortunately
only own the drafts.

But this is all just for the sake of argument: I really don't see the
point of declaring an empty struct. C99 allows forward struct
declarations, and that's probably all we should ever need as far as
incomplete declarations matter.
Ben provided the spec which prohibits memberless struct
definitions in C89, the same holds true for C99 (Section 6.7.2.1).

As I said, these definitions are not so clear about the fact the
lists can be empty or not. You may think otherwise.

Either way, at least GCC was keen enough to see that my code snippet
wasn't doing anything useful at all.

Most other C compilers on the market will choke on the empty struct
declaration, giving, in my experience, a syntax error.
 
J

jacob navia

Guillaume said:
Most other C compilers on the market will choke on the empty struct
declaration, giving, in my experience, a syntax error.

Well, lcc-win32 "chokes" and issues an error. What else do you expect?
This is explicitely forbidden!

After this discussion I added a more explicit error message:
"empty structure" instead of the "syntax error" that lcc-win32 was
issuing before.

jacob
 
G

Guillaume

jacob said:
After this discussion I added a more explicit error message:
"empty structure" instead of the "syntax error" that lcc-win32 was
issuing before.

Well, then it was not completely useless, after all.
:)
 
C

Charlie Gordon

Eric Schmidt said:
(e-mail address removed) (Jeff Mott) wrote in message

Since no one bothers to address the real issue, here is a clue :
It it legal to use a struct tag as a typedef for a pointer to said structure,
but it stinks (C is not Java ;-)
In C++ it is probably incorrect as the struct tag is already type name for the
struct itself.
I assume the following would be OK in both languages :

typedef struct foo foo;

--
Chqrlie.
Let me quote Emmanuel Delahaye:
"It is confusing to hide pointers behind typedefs. A real C programmer (the
hairy, tatooed kind) doesn't do that. He is not ashamed of his pointers, his
shows them off: they are his pride. (wanna check my pointer, baby ?)"
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top