C Pointer

In C language pointer is a derived data type. Pointer contains memory addresses. These memory addresses are the locations in the computer memory where the profram instruction and the data are reserved. So the pointer can access and manipulate the data gathered in the memory location.



Declaration of a Pointer

The general form of declaring a pointer is given below:

Syntax

  1. type *pointer_name;

The asterisk(*) defines that the variable is a pointer variable. For example:

Example

  1. int *p; /* integer pointer */

The variable p declares as a pointer variable that points to an integer data type.



Initialization of a Pointer

After declaration of a pointer you can initialize the variable using assignment operator. For example:

Example

  1. int *p;
  2. int num = 27;
  3. p = #

Here 27 is assigned to the variable num and p pointer variable is assigned by the address of num.



Advantages of Using a Pointer

  • Pointers can handle arrays and data table more efficiently.
  • Without pointers dynamic memory allocation cannot be performed.
  • Pointers can be used to return multiple values from a function using function arguments.
  • Pointers reduce length and complexity of a program.
  • Pointers increase execution speed.
  • Pointers reduce the execution time of a program.


An example of a program using a pointer:

Example:

  1. #include <stdio.h>
  2. int main() {
  3. int num = 27;
  4. int *p;
  5. p = &num;
  6. printf("Address of num variable: %x\n", &num);
  7. printf("Address stored in p pointer variable: %x\n", p);
  8. printf("Value of p pointer variable: %d\n", *p);
  9. return 0;
  10. }

Output:

Address of num variable: 377536b4
Address stored in p pointer variable: 377536b4
Value of p pointer variable: 27