Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact | Share This Page!                      Shop Online

  News
Channels
In Focus

Analysis  
Bolography  
Cartoons
Environment   
Opinion 

Columns
 Business
 My Word 
 PlainSpeak 
 Random Thoughts 
Our Heritage

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

Society & Lifestyle

Family Matters 
Health
Parenting
Perspective 
Recipes
Society
Teens 
Women 

Creative Writings

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

Computing
  General Articles
 
CC++ 
  Flash 
  Internet Security 
 
Java 
 
Linux     
  Networking  

Computing | CC++   
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.     

– Sachin Mehta
March 8, 2001

C/C ++        
Managing Input-Output Operations
     
Managing Output Operations
    
Decision Making And Branching
     
Decision Making and Branching (if statement)
     
Decision Making and Looping
    
The Do Statement
   
Arrays
    
Arrays - Multi-Dimensional
    
Handling of Character Strings
    
Handling of Character Strings - 2  
User Defined Functions   
   User Defined Functions...contd    
Handling of Functions  

Computing
Flash | Internet Security 
Java | Linux | Networking  

General Articles  

Top | Previous | Next  
 


 

Recommend This Page!

Analysis | Architecture | Astrology | Ayurveda | Book Reviews | Buddhism | Cartoons | Cinema | Computing | Culture | Dances
Environment | Fables | Family Matters | Festivals | Hinduism | Health | History | Home Remedies | Humor | Individuality | Jagoji
Literary Shelf | Memoirs | Musings | Opinion | Parenting | Perspective | Photo Essays | Places | Ramblings
Random Thoughts | Recipes | Sikhism | Society | Spirituality | Stories | Teens | Travelogues | Vastu | Vithika | Women

Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact


Boloji.com is owned and managed by Boloji Media Inc

Privacy Policy | Disclaimer
No part of this Internet site may be reproduced without prior written permission of the copyright holder.