Arrays by Sachin Mehta
 Home | Kabir | Poetry | Blogs | BoloKids | Writers | Contribute | Search | Contact | Site Map | Gift Shop | Advertise | RSS | Login | Register
 Channels

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

## Columns

A Bystander's Diary
My Word
PlainSpeak
Random Thoughts

Architecture
Astrology
Ayurveda
Buddhism
Cinema
Culture
Dances
Festivals
Hinduism
History
People
Places
Sikhism
Spirituality
Vastu
Vithika

Family Matters
Health
Parenting
Perspective
Recipes
Society
Teens
Women

Book Reviews
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Musings
Quotes
Ramblings
Stories
Travelogues
Workshop

## Computing

CC++
Computing Articles
Flash
Internet Security
Java
Linux
Networking
Arrays
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, 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.

8-Mar-2001
More by :  Sachin Mehta

Views: 1863
 very nice kapilbamba06/20/2011
Name*
Email ID*  (will not be published)
Comment
Verification Code*
 V5D78
Please fill the above code for verification.

Top | CC++

~*~
by Rajender Krishan

 A Bystander's Diary Analysis Architecture Astrology Ayurveda Book Reviews Buddhism Business Cartoons CC++ Cinema Computing Articles Culture Dances Education Environment Family Matters Festivals Flash Ghalib's Corner Going Inner Health Hinduism History Humor Individuality Internet Security Java Linux Literary Shelf Love Letters Memoirs Musings My Word Networking Opinion Parenting People Perspective Photo Essays Places PlainSpeak Quotes Ramblings Random Thoughts Recipes Sikhism Society Spirituality Stories Teens Travelogues Vastu Vithika Women Workshop