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.

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Seems reasonable, the enum type definition is located somewhere else, yet to be found.
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).
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.

Comments

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;

1 Comment

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.
0

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

See this question, as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.