Wednesday 29 January 2014

Explain array with one dimensional array example

Array-It is a set of unique variables with same data type located in continous memory locations.

Declaration of array
int a[5];
Here set of 5 elements with integer data type is stored in the array a.

initialization of array
int a[5]={1,2,3,4,5}

Important point-
(a)Array elements are called by array names.
array element 1 called by array name a[0],array element 2 called by array name a[1],array element 3 called by array name a[2],
array element 4 called by array name a[3],array element 5 called by array name a[4]
(b)Any particular element of an array can be modified seperately without disturbing other elements.
Example-
int a[5]={1,2,3,8,5}
To carry out this task the statement a[3]=4 can be used to change element 8 to 4 from array a[5].
(c)Starting memory location of array a assumed 2000 then next element located in 2002 because each integer element requires 2 bytes.
So character-1 byte,integer-2bytes,float-4 bytes,long-4 bytes,double-8 bytes.

Character array-They are called as strings.Here NULL('\0') character is automatically added at the end.
NULL acts as an end of the character array when compiler reads it .

Write a program to display elements of one dimensional character array with address.
#include<stdio.h>
#include<conio.h>
main()
{
int i=0;
char name[6]={'A','R','R','A','Y'};
printf("Character and memory location");
while(name[i]!='\0')
    {
        printf("    \n  %c \t %u   ",name[i],&name[i]);
        i++;
    }
}
Output
Character and memory location
A    4054
R    4055
R    4056
A    4057
Y    4058

No comments:

Post a Comment