The greatest mistake you can make in life is to be continually fearing you will make one

Monday 8 March 2010

C Array

Introduction:
  • An Array is a collection of similar elements of same data type. The elements may be all int , or all float or all char.
  • we cant create a array with 5 integer element and 5 float elements. 
  • Array elements are referred by using subscript,the lowest subscript is always 0 and the highest subscript is size-1.The valid range of subscript is 0 to size-1
  • Refering an array element using an out of range subscript will produce an error
  • Array elements occupy consecutive memory locations and the size of each element is same.
  • The array name is a pointer constant.we can pass the array name to the function and manipulate array elements in the function.
  • An array is always processed element by element.
  • The array subscript has the highest precedence among all operators.Thus a[2]++,will increment the value at location 2 in the array
Array Declaration:
        Like other variables array must be declared. The declaration will inform the compiler about the type of array(int,float or char) and the size(Number of elements) of array.
Syntax:


data type array_name[size];
data type-> specifies the type of variable
array_name -> name of the array variable
size->number of elements in the array
[]->tell the compiler this variable is a array

example:
int a[10];
float b[5];
char name[10]; [The array of character is called string]


Accessing Array elements:
  • All the array elements are numbered starting with 0. This number specifies the elements position in the array.The individual elements in the array can be accessed by specifying array name followed by the position of the element(index) in the array.
Syntax:

array_name[index];
In a[10] array,
a[0]->First element of the array.
a[9]->last element of the array.


  • The other way of accessing array elements are:
  1. *(array_name+index)==>*(a+i)
  2. *(index+array_name)==>*(i+a)
  3. index[array_name]==>i[a]
here i is the index

Array Initialization:
The initial value for the array can be given at the time of declaration.

Syntax:



data type array_name[size]={value list};

Ex:

int numbers[5]={10,20,30,40,50};
int num[]={1,2,3,4,5,6};
float count[5]={1.5,3.5,5.6,6.7,7.8};
char vowels[6]={'a','e','i','o','u','/0'};

Note:

  1. The characters must be enclosed in single quote(').
  2. The last character in a char array must be a NULL(/0) character.
  3. We can directly assign the string in to a character array.The string must be enclosed in double quotes("). eg: char vowels[6]="aeiou";
  4. When the value assigned to a character array is a string the compiler automatically supplies a NULL character but we need to specify one extra space for NULL character.
Passing Array Elements to a Function

  • Array elements can be passed to a function by calling the function by value or by reference
call by value:
  • In the call by value,the values of the array elements are passed to the function.
  • The address can be printed using %16lu(decimal digits) or %p(hexadecimal address)
  • Example:Program to pass array elements using call by value
 #include<stdio.h>
void printArray(int a[]);
main()
{
    int i,a[5];
    for(i=0;i<5;i++)
    {
        a[i]=i;
    }
    printArray(a);
    return 0;
}
void printArray(int a[])
{
    int i;
    for(i=0;i<5;i++)
    {
        printf("%d element is at address %16lu\n",a[i],&a[i]);
    }
}
Output
0 element is at address  140737342687264
1 element is at address  140737342687268
2 element is at address  140737342687272
3 element is at address  140737342687276
4 element is at address  140737342687280
  • Since each array element is of the integer type,the difference between addresses is 4
Call by reference:
  • The array members can be passed to the function using call by reference ie passing the base address of the array.Base address is the address of the zeroth element.
  • Each element of the array has a memory address.
  • Example program to passing an entire array using call by reference
#include<stdio.h>
void printArray(int *a);
main()
{
    int i,a[5];
    for(i=0;i<5;i++)
    {
        a[i]=i;
    }
    printArray(&a);
    return 0;
}
void printArray(int *a)
{
    int i;
    for(i=0;i<5;i++)
    {
        printf("%d element is at address %p\n",*a,a);
        a++;
    }
}
output:
0 element is at address 0x7ffffbd6ac50
1 element is at address 0x7ffffbd6ac54
2 element is at address 0x7ffffbd6ac58
3 element is at address 0x7ffffbd6ac5c
4 element is at address 0x7ffffbd6ac60
MORE ABOUT ARRAYS

1 comment:

  1. In line 10, Warning
    Correction,,,

    printArray(&a[0]);
    or printArray(a);

    ReplyDelete