1

I have come across some c code where the there is an enum type followed by a function implementation, such as this:

enum OGHRet funcX ( OGH *info, void *data, int size )
{
    /* c code that does stuff here */
}

I am confused over how this enum statement works inline with the function implementation. I assume it is the return type of funcX, but why is declared explicitly with enum?

Thanks in advance.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345

4 Answers4

2

its just saying its returning an enum called OGHRet which will be defined elsewhere.

Here's a fragment of code that shows enums and functions that return enums side by side...

enum Blah { Foo,  Bar };

enum Blah TellMeWhy()
{
   return Bar;
}
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • Seems reasonable, the enum type definition is located somewhere else, yet to be found. – pillsdoughboy Jan 24 '12 at 21:35
  • 2
    It's saying that the type *has been* defined elsewhere. References to incomplete enum types are not allowed in standard C (though some compilers may permit it as an extension). – Keith Thompson Jan 24 '12 at 21:37
1

If you define an enum type like this:

enum OGHRet { foo, bar };

then enum OGHRet is simply the name of the type. You can't refer to the type just as OGHRet; that's a tag, which is visible only after the enum keyword.

The only way to have a one-word name for the type is to use a typedef -- but it's really not necessary to do so. If you insist on being able to call the type OGHRet rather than enum OGHRet, you can do this:

typedef enum { foo, bar } OGHRet;

Here the enumeration type is declared without a tag, and then the typedef creates an alias for it.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

Maybe because it isn't declared as a typedef, like this:

enum OGHRet
{
 FOO,
 BAR
};

Here you will need to make reference to this by enum OGHRet. To use only OGHRet, you need to do it like this:

typedef enum _OGHRet
{
 FOO,
 BAR
}OGHRet;
Gui13
  • 12,993
  • 17
  • 57
  • 104
  • 1
    Names beginning with an underscore followed by an uppercase letter are reserved for the implementation, it's better to not use them in your code. – Daniel Fischer Jan 24 '12 at 21:32
0

You need the enum keyword to provide a type definition for OGHRet. In C++, you can omit it.

See this question, as well.

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144