473,320 Members | 2,012 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,320 software developers and data experts.

Typedef with GCC/G++

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.
Nov 14 '05 #1
14 4649
Mac
On Thu, 18 Nov 2004 21:46:54 -0800, Jeff Mott wrote:
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

Nov 14 '05 #2

On Fri, 18 Nov 2004, Jeff Mott wrote:

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
Nov 14 '05 #3
mj****@twcny.rr.com (Jeff Mott) writes:
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").

--
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 14 '05 #4
In <97**************************@posting.google.com > mj****@twcny.rr.com (Jeff Mott) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
mj****@twcny.rr.com (Jeff Mott) wrote in message news:<97**************************@posting.google. com>...
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.
Nov 14 '05 #6
er*******@comcast.net (Eric Schmidt) wrote:
struct fubar
{
};


Actually, I should point out that this code is not valid C, because
your struct has no members.


Since when did structs have to have members?
Nov 14 '05 #7
ol*****@inspire.net.nz (Old Wolf) writes:
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 ;

--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Nov 14 '05 #8
>>> struct fubar
{
};


Actually, I should point out that this code is not valid C, because
your struct has no members.

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;
}
-----------------------------------------------------------------------

gcc -c -Wall test4.c
test4.c: In function `main':
test4.c:14: warning: statement with no effect
Nov 14 '05 #9
[someone wrote]
Since when did structs have to have members?

In article <news:41**********************@news.club-internet.fr>
Guillaume <"grsNOSPAM at NOTTHATmail dot com"> 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? :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10
On Sun, 21 Nov 2004 16:29:52 +0100, Guillaume wrote:
struct fubar
{
};

Actually, I should point out that this code is not valid C, because
your struct has no members.

Since when did structs have to have members?


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
Nov 14 '05 #11
> 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.
Nov 14 '05 #12
Guillaume wrote:
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
Nov 14 '05 #13
jacob navia wrote:
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.
:)
Nov 14 '05 #14
"Eric Schmidt" <er*******@comcast.net> wrote in message
news:f8**************************@posting.google.c om...
mj****@twcny.rr.com (Jeff Mott) wrote in message

news:<97**************************@posting.google. com>...
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


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 ?)"

Nov 14 '05 #15

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...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
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...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
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 ! )...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.