Few unusual C/C++ declarations
https://softwareengineering.stackexchange.com/questions/376555
Frage
I came across this Enum and Struct declarations in a project supposedly done by an expert. The declarations / definitions are little different than what im used to so far.
enum EnumKeys {
KEY_MENU ,
KEY_EXIT ,
KEY_DOWN ,
KEY_UP ,
KEY_RIGHT ,
.
.
.
};
But nowhere in the code the actual Enum had been defined by a line of code like
EnumKeys test;
for example. But still the KEY_MENU, KEY_EXIT are freely available just after this declaration. For example, if I type cout << KEY_EXIT
, it prints 1.
Other instance is, this structure declaration / definition.
typedef struct t_ExpoData {
int8_t expNorm;
int8_t expDr;
int8_t drSw;
}ExpoData;
typedef struct t_ModelData {
...
...
ExpoData expoData[4]; // 3*4
}ModelData;
So the way I read this is, there is a new structure with the name ExpoData and one instance of it is ExpoData.
Instead of using that instance right away, why the second declaration ExpoData expoData[4]
?
If anyone understands this, as of what clang specification is this, and how to interpret this correctly, that is much appreciated.
Lösung
But nowhere in the code the actual Enum had been defined by something like EnumKeys test;
For the enumeration, there may never be any declaration of a variable of that type, but it may be used for creating other values. Often in C, enum
types are combined into int
s or long
s. For example, a developer could write:
int bitmask = KEY_UP | KEY_DOWN;
and then use that value in a comparison to check whether the up or down arrow key was pressed.
So the way I read this is, there is a new structure with the name ExpoData and one instance of it is ExpoData.
No, that's not what this means. The typedef
in this style:
typedef struct <someName> {
... fields
} <somePossiblyOtherName>;
is a typical C type definition. They take the form:
typedef <existing type> <new type>;
Furthermore, C struct
definitions can be done without the typedef like this:
struct <typeName> {
...fields
};
You can then use that type by typing struct <typeName>
before a variable declaration, function return type or function argument. Like this:
struct <typeName> functionName(struct <typeName> argument)
where typeName is the name of the type.
So what the type definition in your code is saying is that they are declaring a new type that is a struct t_ExpoData
and they are giving the new type the name ExpoData
. It saves users of the type from having to type struct t_ExpoData
every time they want to use it. Instead they can type just ExpoData
. It could have been written as 2 different lines like this:
struct t_ExpoData {
... fields...
};
typedef struct t_ExpoData ExpoData;
Instead of using that instance right away, why the second declaration ExpoData expoData[4] ?
The author of the code is saying that they want an array of 4 instances of ExpoData
in their ModelData
structure, and they want that field to be called expoData
. You would access it like this:
ModelData someModel;
someModel.expoData[0].expNorm = 5;