473,320 Members | 1,861 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.

resolving a warning error

I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?
Nov 14 '05 #1
11 3205
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void);' instead.

"But I'll lose type safety!" Well, no. In the first
place, there's almost no type safety to begin with: C's
enums are pretty weak. In the second place, if you intend
to hide the enum's values from someFunc(), what will its
`return' statements look like? Where are you going to get
a known-to-be-valid value? Copy it from a global? Bah!

Or perhaps you intend to reveal the enum values to
someFunc() but hide them from someFunc()'s callers. What's
the point of that? It means that all the callers can do
with someFunc()'s returned value is pass it around among
themselves without understanding: They can't perform a
meaningful test with an `if' or a `switch' or any such;
all they can do is hand it from place to place as an
"opaque" value. An `int' is perfectly suited to such use.

`enum anEnum' doesn't seem to add value here.

--
Er*********@sun.com

Nov 14 '05 #2

In article <23**************************@posting.google.com >, po****@gmail.com (michael potter) writes:
I would like to code a prototype that returns an enum, but not declare
the enum.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


You can't. Well, you may be able to do specifically what you ask
in your final question - eliminate one diagnostic from one particular
implementation - but that's off-topic here. What you cannot do is
use an enum without declaring it, in C. C permits incomplete structs
in various contexts; it does not permit incomplete enums. Some
implementations may allow the latter, but it is an extension.

There's a good reason for not allowing incomplete enums - the
implementation is allowed to pick any integral type for an enum,
provided that type can represent all the values of the enum. The
latter are always of type int, but they may fit in the range of, say,
unsigned char, in which case the implementation could use unsigned
char for that enum type.

If the implementation doesn't have a complete definition of the enum,
it may not know what integral type to use for it.

Of course, that doesn't preclude pointers to incomplete enums, just
as you can have pointers to incomplete structs. Why those were
omitted is a mystery to me; I suppose no one on the committee thought
they were sufficiently important.

--
Michael Wojcik mi************@microfocus.com

Pocket #16: A Ventriloquist's "Helper" -- Recordings for Divers Occasions,
especially cries to put in the mouths of enemies -- "God Bless Captain
Vere!" "Les jeux sont faits!" &c. -- Joe Green
Nov 14 '05 #3
In <23**************************@posting.google.com > po****@gmail.com (michael potter) writes:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.
Not if you invoke them in conforming mode:

fangorn:~/tmp 200> gcc test.c
fangorn:~/tmp 201> icc test.c
fangorn:~/tmp 202> gcc -ansi -pedantic test.c
test.c: In function `main':
test.c:8: warning: ISO C forbids forward references to `enum' types
fangorn:~/tmp 203> icc -Xc test.c
test.c(8): warning #102: forward declaration of enum type is nonstandard
enum anEnum someFunc(void);
^
The diagnostic is *required* by the C standard:

6.7.2.3 Tags

Constraints

1 A specific type shall have its content defined at most once.

2 A type specifier of the form

enum identifier

without an enumerator list shall only appear after the type it
specifies is complete.
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


Your only chance is to find a way of invoking xlc in nonconforming mode,
because a conforming compiler MUST generate it, but you *really* don't
want to do that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #4
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void);' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
Dan Pop wrote:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.
[...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void);' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?


No, of course not: 6.7.2.2/4 lists the allowable
types, and not all are necessarily compatible with `int'
on all implementations. However, any of the enumerated
values is compatible with `int' because each *is* an `int'.

It is, of course, possible to store a non-enumerated
value in an enum variable:

enum { FOO = 1, BAR = 3, BAZ = 5 } x = 2;

is legal C, unfortunately. (Note that if 2 were replaced
with 128 or -300 this would not be legal C: the compiler
might have chosen an integer type too narrow for these
non-enumerated values.) But I don't think that's a serious
objection -- after all, what the O.P. wants to do is already
a violation of C's rules, and substituting `int' seems a
reasonable way to begin a return to legality.

--
Er*********@sun.com

Nov 14 '05 #6
Da*****@cern.ch (Dan Pop) writes:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman
<er*********@sun.com> writes:
michael potter wrote: [...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void);' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?


They're assignment-compatible, which is good enough for the problem
under discussion.

Declaring

int someFunc(void);

in foo.h and

enum anEnum someFunc(void) { /* blah blah */ }

in foo.c could cause problems, particularly on systems where the types
have different sizes, but I don't think that's what Eric was
suggesting.

If you consistently treat someFunc as a function returning int, you
can still use enum anEnum internally.

--
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 #7
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
Dan Pop wrote:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
michael potter wrote:

I would like to code a prototype that returns an enum, but not declare
the enum.
[...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?

By using `int someFunc(void);' instead.
Can I have the chapter and verse stating that enumerated types are
compatible with type int?


No, of course not: 6.7.2.2/4 lists the allowable
types, and not all are necessarily compatible with `int'
on all implementations.


Free clue: the concept of compatible types is NOT implementation
dependent. It is the C standard itself that specifies what types are
compatible:

1 Two types have compatible type if their types are the
same. Additional rules for determining whether two types are
compatible are described in 6.7.2 for type specifiers, in 6.7.3
for type qualifiers, and in 6.7.5 for declarators.
However, any of the enumerated
values is compatible with `int' because each *is* an `int'.


Which is entirely irrelevant here: if the function declaration doesn't
match the function definition, the behaviour is undefined:

2 All declarations that refer to the same object or function shall
have compatible type; otherwise, the behavior is undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void);' instead.

"But I'll lose type safety!" Well, no. In the first
place, there's almost no type safety to begin with: C's
enums are pretty weak. In the second place, if you intend
to hide the enum's values from someFunc(), what will its
`return' statements look like? Where are you going to get
a known-to-be-valid value? Copy it from a global? Bah!

Or perhaps you intend to reveal the enum values to
someFunc() but hide them from someFunc()'s callers.


Or neither. He may intend to hide the actual enum definition from
third party entities that neither define nor call someFunc(), they
merely pass its address to functions that have access to the actual
definition of enum anEnum. The logical equivalent of type FILE in
<stdio.h>.

It would be nice if that were possible, but it isn't. He must *define*
someFunc() as returning int, not only declare it as such.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman
<er*********@sun.com> writes:
michael potter wrote:[...] How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?

By using `int someFunc(void);' instead.
Can I have the chapter and verse stating that enumerated types are
compatible with type int?


They're assignment-compatible, which is good enough for the problem
under discussion.


Maybe, but not for the suggested solution.
Declaring

int someFunc(void);

in foo.h and

enum anEnum someFunc(void) { /* blah blah */ }

in foo.c could cause problems, particularly on systems where the types
have different sizes,
It invokes undefined behaviour (unless enum anEnum happens to be
compatible with type int, but no program can make this assumption in
c.l.c):

2 All declarations that refer to the same object or function shall
have compatible type; otherwise, the behavior is undefined.

so I have no idea where you got your "could" and the nonsense about
different sizes from. But maybe you can provide some chapter and verse
that I have missed....
but I don't think that's what Eric was suggesting.
This is effectively what he wrote. He didn't suggest *any* change in the
function definition, did he?
If you consistently treat someFunc as a function returning int, you
can still use enum anEnum internally.


This is true, if by "consistently treat" you mean also defining it like
this.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10
Dan Pop wrote:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
Dan Pop wrote:
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:

michael potter wrote:

>I would like to code a prototype that returns an enum, but not declare
>the enum.
>[...]
>How can I eliminate that warning message without eliminating other
>warning messages and without declaring the enum?

By using `int someFunc(void);' instead.

Can I have the chapter and verse stating that enumerated types are
compatible with type int?


No, of course not: 6.7.2.2/4 lists the allowable
types, and not all are necessarily compatible with `int'
on all implementations.


Free clue: the concept of compatible types is NOT implementation
dependent. It is the C standard itself that specifies what types are
compatible:

1 Two types have compatible type if their types are the
same. Additional rules for determining whether two types are
compatible are described in 6.7.2 for type specifiers, in 6.7.3
for type qualifiers, and in 6.7.5 for declarators.
However, any of the enumerated
values is compatible with `int' because each *is* an `int'.


Which is entirely irrelevant here: if the function declaration doesn't
match the function definition, the behaviour is undefined:

2 All declarations that refer to the same object or function shall
have compatible type; otherwise, the behavior is undefined.


Aha! I think I understand the confusion, and my
elliptical advice was at least partly responsible for
it. I did not mean to suggest that the O.P should
declare his function as returning an int while defining
it to return an enum, but to declare and define it as
returning an int. I didn't say so explicitly, for much
the same reason I don't tell grownups to look both ways
when crossing streets.

Declarations and definitions should agree, hence,
a (substantive) change to one should imply a corresponding
change to the other.

--
Er*********@sun.com

Nov 14 '05 #11
In <cl**********@news1brm.Central.Sun.COM> Eric Sosman <er*********@sun.com> writes:
Aha! I think I understand the confusion, and my
elliptical advice was at least partly responsible for
it. I did not mean to suggest that the O.P should
declare his function as returning an int while defining
it to return an enum, but to declare and define it as
returning an int.
The bit about defining it as returning an int was completely missing
from your post.
I didn't say so explicitly, for much
the same reason I don't tell grownups to look both ways
when crossing streets.


Yet, you treated the OP as a "kid" all over your post, all the points
you've made being trivial for "grownups".

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #12

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

Similar topics

2
by: Jean Brouwers | last post by:
Sporadically, a message like this one shows up in our wxPython application: (main.py:3182): Gtk-WARNING **: gtk_menu_attach_to_widget(): menu already attached to GtkMenuItem Is possible but...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
1
by: olduncleamos | last post by:
Hi all, Just wondering if it is possible for a custom control to emit compiler warning or even error? It would be great to flag the user at compile time if they made some inconsistent property...
1
by: Nathan Sokalski | last post by:
Visual Studio 2005 unexpectedly stopped generating the *.designer.vb files for *.aspx and *.ascx files. After a few days of frustration trying to fix this, I noticed that it had the following...
6
by: David Mathog | last post by:
Do any of you happen to have links to compendiums (or heaven forbid, an actual manual) which maps compiler warnings and errors to examples of actual code problems? In this case I'm specifically...
10
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
20
by: jacob navia | last post by:
Consider this code static typedef struct { int boo; } FOO; This provokes with MSVC: ------------------------------ Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64...
3
by: santhanalakshmi | last post by:
Hi, <?php mysql_connect('localhost','root',''); mysql_select_db('test'); $query="select * from phonedirectory"; $result=mysql_query($query); echo "One Way To Print"; echo "<br>";
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...
1
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
0
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.