Search code examples
cenumsuser-defined-types

Name of a function return type as name of user define data type


Is it legal to name a function same as that of a user defined data type with enum ,in C? For Example:

enum sentence_id sentence_id(const char *sentence, bool strict);

and enum sentence_id is defined as given below

enum sentence_id {
MINMEA_INVALID = -1,
MINMEA_UNKNOWN = 0,
MINMEA_SENTENCE_RMC,
MINMEA_SENTENCE_GGA,
MINMEA_SENTENCE_GSA,

};  

Is it applicable to other user defined data type(as structure)?


Solution

  • This only looks strange, because the identifier for the enumeration and the function are identical, which is possible, because a enumeration identifier always has to follow an enum keyword and an enum always is followed by an identifier.

    So let's give them different names:

    enum minmea_sentence_id_e 
    {
    …
    }; 
    

    This defines an enumeration with the name minmea_sentence_id_e.

    The function gets another identifier, too:

    enum minmea_sentence_id_e minmea_sentence_id_f(const char *sentence, bool strict);
    

    Now it should be quite clear that there is a function called minmea_sentence_id_f returning a value of enumeration named minmea_sentence_id_e.