### Difference between Arrays and Enumerated Constants

I'm currently learning about arrays and just just finished the lesson on variables. It seems to me that arrays also list multiple values beginning with 0.

I need help with understanding the difference between enumerated constants and arrays :)
Last edited on
Think of enumerated type like of some other form of counting instrument. Eg. integers allow you to represent the quantity of fingers on your hand (5) or number of your legs (2). Chars allow you to represent the first letter of your nick ('s') or of mine ('j');
What datatype is best suited to represent a day of the week we have today? Well, not really any. Except if you define your own
 ``12345`` ``````enum Weekday { MONDAY, TUESDAY, //... };``````

Using enum you define a finite set of allowed values for some kind of reality fragment. They are given numerical value (just like chars are, because computers handle numbers best).
You can then use them in your code:
 ``12345`` ``````int x = 1; char myInitial = 'j'; Weekday today = TUESDAY; if(today == SATURDAY) party();``````

Arrays on the other hand are containers - they are boxes with some number of comartments, and in each compartment you may put a value. If array has 10 elements of type int, you may store 10 different values in the same variable. To be able to reach a compartment you need, you must access it by its index, like this:
 ``12`` ``````int array[10] // Create array with 10 compartments to hold integers array[7] = 555; // put value 555 in seventh eighth compartment ``````

Can it be said any clearer ;) ?
Last edited on
Defined constants just make it easier to write code by substituting a label (like January) with a number (like 1, for the first month) when the code compiles. Enumerations just let you easily define a bunch of constants (give a word label to a number). Arrays on the other hand are containers for data (variables) which can be manipulated, accessed, overwritten etc.

One is a label for fixed data to make it easier to type, the other is a container for data that can be manipulated.
The enumerated type is a way of giving human-readable names to a group of constants. Although by default the numeric values start from 0, there is no obligation that it must do so, nor must the values be consecutive.

In some cases the numeric value is not important at all, it is the name which we are interested in. One thing which makes the enum useful is that it makes the code easier to read, as there is no need to remember that a particular number has a particular meaning, the choice of suitable naming makes it self-explanatory.

An array - it is just a container for some sort of data, usually variable rather than constant. Its purpose and usage is quite different.

Thanks for the quick replies :D This forum is a great help
Topic archived. No new replies allowed.