What is the difference between array and enum in C ?

22,359

Solution 1

Array is a variable that can contain multiple elements with index starting from 0 whereas enum is an user defined datatype that contains a list of members for which an integer constant is assigned starting from 0. in case of enum the numbers starting from 0 are not indexes whereas in case of an array they are indexes. Also in case of enum you can assign your own constant values to the members that may or may not start from 0 and may or may not be in a sequence.

Solution 2

An Enum is basically a group of named constants. It is an alternative to numbered flag parameters. (It also doesn't have to be numbered from zero, you can specify the numbering.)

An Enum could be days of the week for example. A Enum could departments in a company: eg SALES, BILLING, HR ...

A array is a sequence of memory locations. It is a collection. Each element in that collection is indexed by a number. So using that number you can retrieve the value stored at that location. Much like the page number in a book lets you look up the content of that page, the index on an array lets you look up the value stored at that location.

For example, if your company had numbered physical mail boxes for each department (starting from zero): and you were creating some very simple software to let users log in to check how many letters they had uncollected, you might choose to store them in an Array of Ints. Where the index is the mail box number, and the value is the number of letters in the box.

Then for ease of programming, you might choose define the departments as Enums (as described before) such that you could index of department name. But that would be getting more advanced.


You may be confusing Enum, with Enumerable which is a term used in some languages to describe any collection type which can be iterated though in sequence (Eg IEnumerable in C#). This term is not often used in C. It is not in anyway the same at all as Enum.

Solution 3

The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value. It doesn't contain any other values.

Solution 4

Both of them are totally different. Array is a value and enum is a type. array is a collection of different values whereas enum value is simply one value. Array is used to iterate among various values using index whereas enum is assigned some atomic value and iterated so that we can easily iterate the type.

Share:
22,359
Aishwarya
Author by

Aishwarya

I like to develop software applications, especially for web and for mobile.

Updated on June 17, 2020

Comments

  • Aishwarya
    Aishwarya almost 4 years

    I find enum to be similar to an array,with the elements in it being numbered from 0. So what is the difference between an array and enum?