Difference between enum and struct?

Both seem to be richer values than your traditional int and strings, both store “stuff” in it. I don’t necessarily understand the main difference between the two. I checked several posts outside of Udemy to find the answer but it was too confusing to understand. Can anyone explain me the difference between struct and enum in simple terms?

1 Like

Hey mate I will do my best to explain this to you :slight_smile:

First you need to properly understand the difference between a TYPE and an IDENTIFIER

So int32 myNumber = 0;

“int32” is the TYPE.
“myNumber” is the IDENTIFIER.
“0” is the VALUE.

So a struct allows multiple VALUES of a certain TYPE to be stored in a single IDENTIFIER.

Enum allows you to create a new TYPE and define what the values for this TYPE can be.

For example, here are some existing TYPE’s and what type of value they accept.

‘int’ is a type and can be any whole number. (-2, -1, 0, 1, 2…)
‘float’ is a type and can be any fractional number. (-0.13452, 0.5768, 2.1, 6.0…)
‘char’ is a type and can be a character. (a, b, c, d, e, f, g…)
‘string’ is a type and can be any set of characters. (Hi, Hello, Yay!, Awesome…)

So we can create a new type with Enum. So for example;

enum color {red, black, orange, blue, yellow}

‘color’ is a type and can be any of the colors we defined (red, black, orange, blue, yellow).

So I’ll use the video example:

   enum EWordStatus

We have created a new type EWordStatus that has 2 values. OK and Not_Isogram.

If we did this with a struct it would look more like…

struct FWordStatus
	int32 OK = 0;
	int32 Not_Isogram = 1;

We haven’t created a new TYPE. We’ve just stored an existing type (int) into two IDENTIFIERS. Because these are of type int, they can return any value that is an integer. This is not ideal, especially if only ever want there to be 2 values, instead we get 2,147,483,647 values <- (this is the maximum values that can be stored in a 32bit integer.)


I found this to be a really good explanation(Ink2019’s comment): http://www.cplusplus.com/forum/beginner/44859/#msg243304


Thank you so much for the explanation! It really cleared a lot of things in my head! I appreciate your explanation!

1 Like

Thanks for the link, it really helped to verify Simon_Guy’s explanation!

1 Like

Hello @MEGARAGE_9000,

you don’t need to instantiate a enumeration because enum is static. You can directly access the members of an enumeration via the name of the enumeration, i.e. MyEnum::OK;. A struct instead must be first instantiated via MyStruct iStruct = {};. Then you need to interact with the struct-variable, i.e. std::string iString = iStruct.iStatus;. This is more complex.

Kind regards

Privacy & Terms