+ 8
An enum contains labels for constant values (not changing) it's similar to: #define VALUE_A 0 #define VALUE_B 1 #define VALUE_C 2 Enums most often contain a group of related constants where the value of its first member is 0 and increases by one for each constant in the enum (by default). You can assign a value to an enum member and the value of the next member will be one higher. An array is a block of contiguous memory that can be of any variable type: char, int, float, etc and its values can be changed throughout your program. If uninitialized the array will contain garbage values, unlike the enum. Both are often used together. Say you have 3 strings which specify colors red, blue and green in hexadecimal and an enum that contain the name of each color: enum color_names { RED, GREEN, BLUE }; const char *colors[3] = { "#ff0000", "#00ff00", "#0000ff" }; If you want to get the hex number for the color green, instead of doing: printf("Green = %s\n", colors[1]); you can do: printf("Green = %s\n", colors[GREEN]); Which is much more clear and easier to understand.
27th May 2019, 11:58 PM
Cluck'n'Coder
Cluck'n'Coder - avatar