Friday 6 March 2015

Arrays in C

Arrays are a series of elements of the same data type placed consecutively in memory that can be individually referenced by adding an index to a unique name. Using an array we can store five values of type int with a single identifier without having to declare five different variables with a different identifier. Arrays are useful when you store related data items, such as grades received by the students, sine values of a series of angles, etc. Like any other variable in C an array must be declared before it is used. The typical declaration of an array is:

data-type array-name[no-of-elements];

Notice that the array name must not be separated from the square brackets containing the index. When declaring an array, the number of array elements should be constant. As arrays are blocks of static memory locations of a given size, the compiler must be able to determine exactly how much memory to allocate at compilation time.

No comments:

Post a Comment