POINTERS


pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

Concept of pointer

Consider the declaration,

int i=3;

This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
Here the memory mapping is depicted:


We can access the value 3 by either using the variable name i or the address 65524. Since the memory addresses are simply numbers they can be assigned to some other variable. The variable that holds memory address are called pointer variables. A pointer variable is therefore nothing but a variable that contains an address, which is a location of another variable. Value of pointer variable will be stored in another memory location.


Here j is not an ordinary variable like any other integer variable. It is a variable that contains the address of i.
Pointer is a variable that contains address of another variable. Now this variable itself might be another pointer. Thus, here a pointer contains another pointer’s address.


int i, *j, **k ;
Here i is an ordinary int, j is a pointer to an int (often called an integer pointer), whereas k is a pointer to an integer pointer.

EXAMPLE:

#include <stdio.h>
main( )
{
int i = 100, *j, **k ;
j = &i ;
k = &j ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}


Declaration & Initialization  of pointer variables

 Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Type of a pointer must be same as the data type of a variable to which the pointer variable is pointing. void type pointer works with all data types, but is not used often used.
Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator & is used to determine the address of a variable.The other pointer operator available in C is *, called value at address operator. It gives the value stored at a particular address. The value at address operator is also called indirection operator.
#include <stdio.h>

int main () {

   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* pointer variable initialization*/

   }

EXAMPLE:

#include <stdio.h>

int main () {

   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}

Pointer with functions

There are two types of function calls—call by value and call by reference. In the first method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function(call by value).
In the second method (call by reference) the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them. 

EXAMPLE:

include <stdio.h>
void swap(int *a, int *b);

int main()
{
    int m=10, n=20;
    printf("m = %d\n",m);
    printf("n = %d\n\n",n);

    swap(&m,&n); //passing address of m and n to the swap function
    printf("After Swapping:\n\n");
    printf("m = %d\n",m);
    printf("n = %d",n);
    return 0;
}

void swap(int *a, int *b)//pointer a and b holds and points to the address of m and n
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

Comments

Popular posts from this blog

INTERFACING SEVEN SEGMENT DISPLAY WITH 89c51

INTERFACING OF LCD WITH 89C51