C Programming Pointers

Pointers are the powerful feature of C and (C++) programming, which differs it from other popular programming languages like: java and Visual Basic.

Pointers are used in C program to access the memory and manipulate the address.

Reference operator(&)

If var is a variable then, &var is the address in memory.


/* Example to demonstrate use of reference operator in C programming. */
#include <stdio.h>
int main(){
  int var=5;
  printf("Value: %d\n",var);
  printf("Address: %d",&var);  //Notice, the ampersand(&) before var.
  return 0;
}

Output

Value: 5 
Address: 2686778

Note: You may obtain different value of address while using this code.

In above source code, value 5 is stored in the memory location 2686778. var is just the name given to that location.

You, have already used reference operator in C program while using scanf() function.

scanf("%d",&var);

Reference operator(*) and Pointer variables

Pointers variables are used for taking addresses as values, i.e., a variable that holds address value is called a pointer variable or simply a pointer.

Declaration of Pointer

Dereference operator(*) are used to identify an operator as a pointer.

data_type * pointer_variable_name;
int *p;

Above statement defines, p as pointer variable of type int.

Example To Demonstrate Working of Pointers


/* Source code to demonstrate, handling of pointers in C program */
#include <stdio.h>
int main(){
   int *pc,c;
   c=22;
   printf("Address of c:%d\n",&c);
   printf("Value of c:%d\n\n",c);
   pc=&c;
   printf("Address of pointer pc:%d\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   c=11;
   printf("Address of pointer pc:%d\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   *pc=2;
   printf("Address of c:%d\n",&c);
   printf("Value of c:%d\n\n",c);
   return 0;
}

Output

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784
Content of pointer pc: 22

Address of pointer pc: 2686784
Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

Working of pointers in C programming

Explanation of program and figure

  1. Code int *pc, p; creates a pointer pc and a variable c. Pointer pc points to some address and that address has garbage value. Similarly, variable c also has garbage value at this point.
  2. Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the memory location of variable c.
  3. Code pc=&c; makes pointer, point to address of c. Note that, &c is the address of variable c (because c is normal variable) and pc is the address of pc (because pc is the pointer variable). Since the address of pc and address of c is same, *pc (value of pointer pc) will be equal to the value of c.
  4. Code c=11; makes the value of c, 11. Since, pointer pc is pointing to address of c. Value of *pc will also be 11.
  5. Code *pc=2; change the contents of the memory location pointed by pointer pc to change to 2. Since address of pointer pc is same as address of c, value of c also changes to 2.

 

Commonly done mistakes in pointers

Suppose, the programmer want pointer pc to point to the address of c. Then,

int c, *pc;
pc=c;  /* pc is address whereas, c is not an address. */
*pc=&c; /* &c is address whereas, *pc is not an address. */

In both cases, pointer pc is not pointing to the address of c.