Monday, April 9, 2012

Relation between pointers and arrays in C

In Programming langauge C, there is a strong relation between pointers and Arrays. Lets see them in detail. starting fromt the definition.

Array: Is a sequence of memory to store the similar data type. Using index or position, we can access the data from array.
Pointer: is a variable pointing to some memory location. Using this we can access the memory directly.

Most people think that, pointers and arrays are same. but its not true. Some times they are equivalant depending on the context and ussage.  Indexing and pointer arithamatic works on both pointers and arrays. So these are same for both. See the below sample code.

#include
main()
{
    char arr[]="hello";
    char *ptr = arr;
    printf("%c %c \n",arr[3],ptr[3]);
    printf("%c %c \n",*(arr+4),*(ptr+4));
}

Output:
l l
o o

From the above code, accessing the content using indexing is legal for both pointers and arrays. In that code starting address of the array is assigined to a pointer. So both pointing to the same address.


Graphical represetation Of memory using array and pointer:
Memory represetaion of array and pointer

From the above image, it is clear that array stars from the index zero, where as pointer stores the address of the first index in the array.

Pointer Arithmatic: It is legal  to perform arithamtic add/subtraction on pointer variables. Lets compare array with pointer for this with example.

char arry[]="hello";
char *ptr;
ptr=&arry[0];


From the abvoe code snippet,  adrress of the arry index zero is assinged to ptr. Now both ptr and arry[0] are pointing to the same element.  Its perfectly legal to access the next element from ptr location using statement  *(ptr+1). ptr+1 points to the next elemnt of the ptr and * indicates access the value. Similarly ptr-1 points to the previous element from the ptr. Like this array index also works similarly. zeroth element is *(arry+0)  and 1st element is *(arry+1) and nth element is *(arry+n). Using mathematics assosiate law we can write (arry+a) as (1+arry). So its legal to access the 1st element using 1[arry] statement.
There is one basic difference between array and pointers. Pointer is a variable. So below statements are true for pointer.
ptr=arry; // assinging array to the ptr
ptr++; // incrementing the ptr, which points to the next element after increment.
But array is not a variable , so these operations are invalid.
arry=ptr; //illegal
arry++; // illegal, because arry is contant pointer,

Passing as arguments: Arrays passed to the functions converted to pointers. see the sample code below.

void arry_ptr(char arr_arg[],char *ptr_arg) // one is as n array n other is a pointer
{
    char a = arr_arg[4];
    char b = ptr_arg[4];
    printf("a is %c\n",a);
    printf("b is %c\n",b);

}
main()
{
    char arr[]="hello";
    char *ptr;
    char *ptr1;
    char *ptr2;
    ptr=&arr[0];
    arry_ptr(arr,ptr); // passing arr n ptr as arguments
}


OutPut:
a is o
b is o

In the above code, we passed arr and ptr  passed as a function arguments and accessed the same position element in the function. we got same output for both array and pointer. So when passing array as a function arg, compiler will convert it into pointer.

No comments:

Popular Posts