Pointers in C language

10 min read

Pointer: A pointer is a variable that stores the address of another variable.

Advantages of using pointers:

  1. Saves memory space.
  2. Execution time is faster with pointers because data is manipulated with the address.
  3. Essential for dynamic memory allocation.
  4. Helps to return multiple data items from a function (ie., if pointers are passed as arguments).
  5. Helps to pass only a portion of an array to a function (Ex: if the last 50 elements of a 100-element array alone, has to be passed to a function, the address of the .50 element can be passed).
  6. Useful for creating data structures such as linked lists, trees, graphs and so on.
  7. Leads to more compact and efficient code.

For every variable that we use in the program, the compiler assigns some contiguous bytes of memory. Assume 'x' is an integer variable and is allocated 4 bytes of memory, starting from location 4086.  If 'x' is assigned the value 10, the value '.10' gets stored in the memory space reserved for 'x'. (ie., from 4086 to 4089 address). Now, to access the value 10, later on, there are 2 possible ways.

  • usual method - using the variable's name itself.

      Ex:  printf("%d",x);   //This would access the value '10'

  • another way is with the help of its starting address. If the starting address is known for a variable, then its value can be easily accessed.

 

Declaration of a pointer variable: Similar to the declaration of ordinary variables, except that a pointer variable should be preceded by an asterisk symbol '*'  while declaring. Its data type should be same as that of the variable whose address has to be stored. For example, If the address of an 'int' variable has to be stored, the pointer should also be of type 'int'.

If a pointer variable is needed to store the address of a float variable, then the pointer variable should also be a float.

Example: 

int x;    //x is an integer variable.

int *p   //p is a pointer variable as it is preceded by *. Its type is int since it will be used to store the address of x, which is an integer.

p=&x;   //the starting address of x is stored in p.

 

Accessing the value of a variable via pointer:  Adding '*' before a pointer variable helps to fetch the value stored in that address.

  • Let us consider the previous example, where 'p' contains the value '4086' which is the starting address of 'x'. Thus, a statement like printf("%d",p); would give the output '4086'.
  • If the value of 'x' (10) has to be accessed via `p', a statement like printf("%d".*p): would serve the purpose. This produces the output 10.
  • '*p' means the value stored in the address mentioned or referenced by 'p'. Hence is called the 'indirection' or `dereference' operator.

Example: Write a program to add numbers using pointers

int a,b,c;
int *p1,*p2;
p1=&a;   //store address of a in p1
p2=&b;  //store address of b in p2
scanf("%d%d", p1,p1);
c=*p1+*p2;
printf("%d", c);

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error