Tuesday, June 8, 2010

*ARRAYS

Arrays

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, int value[20]; declares the value to be an array of integers containing 20 integer elements.

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, char address[50]; declares the address as a character array variable that can hold a maximum of 50 characters

The last element position is always occupied by a null character like '\0', 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.

Ravinder Singh
*ks*

No comments:

DESCLAIMER:

KIMONGSHAW (RAVINDER SINGH c/o Principal, GDC Chamba, Distt Chamba himachal pradesh 176311 india) ACKNOWLEDGES THAT THOUGH i TRY TO REPORT ACCURATELY, i CANNOT VERIFY THE ABSOLUTE FACTS OF EVERYTHING POSTED. POSTINGS MAY CONTAIN FACT, SPECULATION OR RUMOR. i FIND IMAGES FROM THE WEB THAT ARE BELIEVED TO BELONG IN THE PUBLIC DOMAIN. IF ANY STORIES OR IMAGES THAT APPEAR ON THE SITE ARE IN VIOLATION OF COPYRIGHT LAW, PLEASE EMAIL or call me AND i WILL REMOVE THE OFFENDING INFORMATION AS SOON AS POSSIBLE. contact me kimongshaw@yahoo.co.uk