Operations On Pointers


Operations On Pointers
Just like variable, C allows us to perform several operations on the pointer variables. You can add or subtract an integer from pointer and subtract one pointer from another, increment(++) or decrement(--) the pointers, compare pointers etc. These operations are mostly used on the elements stored in contiguous memory locations such as arrays.
  Now let us take a close look on each of these operations. In our example, we assume the following declarations,
int a[5] = {10, 20, 30, 40, 50}
int *ptr = &a[0];
The first declaration declares an integer array a of size 5 and initializes it such that element a[0] is initialized to 10, element a[1] is initialized to 20 and so on. These elements are stored in contiguous memory locations such as that the first element is assumed to be stored at location 65480.
    The second declaration declares a pointer variable ptr and initialize it with the address of first element in the array.

Adding Integer to a Pointer
The + operator is used to add an integer to a pointer or vice versa. When an integer is added to a pointer variable, the value of the pointer variable is incremented by value of the integer specified multiplied by the size of the pointer’s data type. So when a numeric value n is added to a pointer variable as
<pointer_variable> = <pointer_variable> + n
The compiler will perform the following conversion during pointer addition.
<pointer_variable> = <pointer_variable> + <sizeof data type of pointer variable>*n
For example, the statement
Ptr = ptr + 2;

Subtracting An Integer From a Pointer
The   (Subtraction) operator is used to subtract an integer from a pointer, the pointer has to be the first operand. When an integer is subtracted from a pointer variable, the integer value specified is multiplied by the size of the pointer’s data type and the result is subtracted from the original address. So when a numeric value n is subtracted from a pointer variable as
<pointer_variable> = <pointer_variable> - n
The compiler will perform the following conversion during pointer subtraction.
<pointer_variable> = <pointer_variable> - <sizeof data type of pointer variable>*n
For example, suppose ptr2  points to the fourth element in the array a (i.e. ptr2 contains the address contains the address of element a[3] ) then the statement
Ptr2 = ptr2 - 1

Increment a Pointer
The ++ operator is used to increment a pointer. incrementing a pointer (i.e. adding 1 to a pointer) causes the value of the pointer to be incremented by the size of the pointer type. So if ptr is a pointer variable containing the address of the first element of the integer array a.
Ptr++;   /*equal to ptr = ptr + 1  */
  Causes the value of the ptr to be incremented by the size of the array type int (2 bytes) and ptr now points to the next array element in the array.
Decrementing a Pointer
The - - operator is used to decrement a pointer. Decrementing a pointer (i.e. subtracting 1 from a pointer ) causes the value of the pointer to be decremented by the size of the pointer type. So if ptr2 points to the fourth element in the array a (i.e. ptr2 contains the address of element a[3] ) then the statement
Ptr2--;   /*equal to ptr = ptr - 1  */
  Causes the value of the ptr2 to be declared by the size of the type int (2 bytes) and ptr2 now points to be previous element in the array.
Subtracting One Pointer Variable From Another
 You can also subtract one pointer from another provided both point to the same data type. The result of subtraction is an integer number which indicates the number of elements present between two pointer variable.
Comparing Pointers
Pointer variables can also be compared provided both of them point to the members of the same data structures such as arrays. The relational operators (>, >=, <, <=, ==, !=) can be used to compare the pointers.

No comments

Powered by Blogger.