Sunday, July 15, 2012

Arrays

Hi friends..... ,

We have already talk about simple data types, simple variable can hold only one value at any time during program execution ,although that value is change. We use data structures to save multiple values at the same time.The array is a one kind of data structure.

Lets see about Arrays.........


Arrays of any type can be formed in C. The syntax is simple:
type name[dim];
An array is a group of related data items that all have the same name and the same data type.
Arrays can be of any data type we choose.
An array’s data items are stored contiguously in memory.
Each of the data items is known as an element of the array.  Each element can be accessed individually.


In C, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C treats the name of the array as if it were a pointer to the first element--this is important in understanding how to do arithmetic with arrays. Thus, if v is an array, *v is the same thing as v[0], *(v+1) is the same thing as v[1], and so on:



int a[5];
An array as a whole has the following properties:
Its elements are contiguous in memory.




                                                                                                                                                


Accessing and Initialization of Array Elements


Each element in an array has a subscript (index) associated with it.
Values of individual elements can be accessed by indexing into the array.  For example,
printf(“The element = %d.\n”, numbers[ 2 ] ) ;

Individual elements of an array can also be modified using subscripts.
numbers[ 4 ] = 20 ;

Initial values may be stored in an array using indexing, rather than using an array initialization.
numbers[ 0 ] = 85 ;
              numbers[ 1 ] = 52 ;
numbers[ 2 ] = 36 ;
numbers[ 3 ] = 79 ;
numbers[ 4 ] = 93 ;



Since many arrays are quite large, using an array initialization can be impractical.
Large arrays are often filled using a for loop.
  for ( i = 0; i < 1000; i++ )
  {
     values [ i ] = 0 ;
  }
would set every element of the 1000 element array “values” to 0.



Array Declarations

int numbers[ 5 ] ;
This declaration sets aside a chunk of memory that is big enough to hold 5 integers.
Besides the space needed for the array, there is also a variable allocated that has the name of the array.  This variable holds the address of the beginning (address of the first element) of the array.


number -> AB001 (memory location)
&number[0] -> AB001

Try this example , U can clearly understand what i talk above ...



#include <stdio.h>
int main( void )
{
    int numbers[ 5 ] = { 97, 68, 55, 73, 84 } ;
    printf (“numbers[ 0 ] = %d\n”, numbers[ 0 ]) ;
    printf (“numbers = %X\n”, numbers) ;
    printf (“&numbers[ 0 ] = %X\n”, &numbers[ 0 ]) ;
    return 0 ;
}

How Indexing Works



numbers[ 2 ] = 7 ;
The element assigned the value 7 is stored in a memory location that is calculated using the following formula:



location = (beginning address)+ ( index*size of(array type))





No comments:

Post a Comment