Nov 26, 2024
Nov 26, 2024
by Sachin Mehta
We have seen the various decision-making statements in the last few articles. Till now we have also seen only the basic or implicitly available variables for the data types. Now we would move on to the concept of arrays in C.
An array can be seen as a single name under which there are a number of related data items. For instance if we say x is an array of 10 integers we mean to say that each of the variables from x[1] to x[10] can occupy a single integer value. Now the individual elements of the array x are called the elements of the array x. Thus x[1], x[2],.. are the elements.
The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables us to develop concise and efficient programs. The whole concept might sound to confounding to beginners, but as we move on things will seem to become clear.
ONE DIMENSIONAL ARRAY
A list of items can be given one variable name using only one subscript (i.e. the integer value or length of the array in between the square braces).
Suppose we want to represent 3 integers 23, 24, 25 using arrays, we declare int x[3]; .
The computer reserves 3 storage locations as shown below:
________
x[0]----------- |________|
________
x[1]------------ |________|
________
x[2]------------ |________|
Now these can be treated just like your normal integer variables in assignment or output or expressions. The storage can be assumed from either 0th position or the 1st position.
The subscript of an array can be integer constants, integer variables like i, or expression that yield integers.
Declaration of one-dimensional arrays
Unlike Pascal C and C++ both provide an easy method to declare the arrays. The general form of array declaration is
type variable-name[size];
The type specifies the type of element that will be contained in the array, such as int, float, or char and the size indicates the maximum number of elements that can be stored inside the array .For example,
The C language treats character strings as array of characters. Thus the size in this case will indicate the maximum number of characters the string variable can hold.
For example,
The last element position is always occupied by a null character like '', so the size should be chosen one more than the actual string being stored.
Initialization of one-dimensional arrays
Just similar to the ordinary variables the array elements can also be initialized. The general form of initialization is:
type array-name[size] = { list of values};
Commas separate the values in the list. For example
int id[3] = {0,0,0);
will declare the variable id as an array of 3 integer elements and assign 0 to each element. If the size of the array is more than the elements in the braces then the rest elements are automatically initialized to zero.
The size could be omitted in the initializations, in that case the compiler allocates required space to the array. For example
int count[] = {3,6,8.5};
char name[] = {'S', 'A','C'};
We will continue our discussion on arrays in the next article. Arrays find application in almost all C/C++ programs, so be sure to join in then.
08-Mar-2001
More by : Sachin Mehta
very nice |