Skip to main content

16. Pointers

 

C Pointers

The pointer in C language is a variable which stores the address of another variable. This variable (pointer) can be of the type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Declaring a Pointer

The pointer in C language can be declared using * (asterisk). It (*) is also known as indirection operator used to dereference a pointer i.e. used to get the value at a given address.

#include <stdio.h>

int main()
{
   int num = 50, *p;
   p = &num;
   printf("Address of num variable is %x\n", p);
   printf("Value of p variable is %d\n", *p);
   return 0;
}

Output :

Address of num variable is 61fec8
Value of p variable is 50

Pointer to Array

int arr[10];
int *p[10] = &arr;

Pointer to Function

void show (int);
void(*p)
(int) = &display

Pointer to Structure

struct st {
   int i;
   float f;
}ref;
struct st *p = &ref;

Advantages of Pointers

  1. Pointer reduces the code and improves the performance, it is used for retrieving strings ,trees etc. and used with arrays, structures, and functions.
  2. We can return multiple values from a function using the pointer.
  3. It makes us able to access any memory location in the computer's memory.

Usage of Pointers

  1. Dynamic memory allocation
    In C language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
  2. Arrays, Functions and Structures
    Pointers in C language are widely used in arrays, functions, and structures. It reduces the code, and improves the performance.

Address of (&) Operator

The address of operator '&' returns the address of a variable. But, we need to use %u or %x to display the address of a variable.

#include <stdio.h>

int main()
{
   int n = 50;
   printf("Value of n is %d\nAddress of n is %u\nAddress of n is %x", n, &n, &n);
   return 0;
}

Output :

Value of n is 50
Address of n is 6422212
Address of n is 61fec4

Pointer program to swap two numbers without using the third variable

#include <stdio.h>

int main()
{
   int a = 10, b = 20, *p1 = &a, *p2 = &b;
   printf("Before swap : *p1 = %d\n*p2 = %d\n", *p1 ,*p2);
   *p1 = *p1 + *p2;
   *p2 = *p1 - *p2;
   *p1 = *p1 - *p2;
   printf("After swap : *p1 = %d\n*p2 = %d", *p1, *p2);
   return 0;
}

Output :

Before swap : *p1 = 10
*p2 = 20
After swap : *p1 = 20
*p2 = 10

Reading Complex Pointers

The precedence and associativity of the operators which are used regarding pointers.

OperatorPrecedenceAssociativity
(), []1Left to Right
*, identifier2Right to Left
Data Type3-
  • () : This operator is a bracket operator used to declare and define the function.
  • [] : This operator is an array subscript operator.
  • *  : This operator is a pointer operator.
  • identifier : It is the name of the pointer. The priority will always be assigned to this.
  • Data Type : Data type is the type of the variable to which the pointer is intended to point.

How to read the pointer : (*p)[10]

The pointer will look like the following :

  • p     - 1
  • *      - 2
  • [10] - 3

The pointer will be read as p is a pointer to an array of integers of size 10.

C Double Pointer (Pointer to Pointer)

In C, we can define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.

#include <stdio.h>

int main()
{
   int a = 10, *p, **pp;
   p = &a;
   pp = &p;
   printf("Address of a : %x\n", &a);
   printf("Address of p : %x\n", pp);
   printf("Address of pp : %x\n", &pp);
   printf("Value stored at p : %d\n", *p);
   printf("Value stored at pp : %d\n", **pp);
   return 0;
}

Output :

Address of a : 61fec8
Address of p : 61fec4
Address of pp : 61fec4
Value stored at p : 10
Value stored at pp : 10

Comments