Pointers in C/C++ with examples

Till now, we had only used data types that work upon data on the outer layers of the system. But in some cases, we are required to operate on the inner layers, like working on memory blocks to store and analyze the data. In such cases Pointers are very much helpful, they not only help in managing memory dynamically but also help in creating dynamic data structures which get memory at runtime in the heap. They help in handling parameters sent to functions, access data stored in arrays, etc by just a simple concept of pointing towards the memory address of a variable.


To completely understand the concept of pointers in C, we need to have prior knowledge about how to access the address of the variable, and how to fetch the value stored at that address, as these are the basics on which the whole concept of pointers work.

What is a pointer in C?

Pointers are a special type of variable that store the address or the memory location of another variable that has the same data type as that of the pointer variable. The only difference is that the pointer variables have an asterisk (*) symbol at the variable name starting. 

examples:

int *p  ->  pointer to int
char *ptr  -> pointer to char
float *a -> pointer to float

This asterisk(*) is also known as an indirection symbol, which is also useful in fetching out the value at a particular memory address. This process is known as dereferencing.

Accessing the address of a variable in C/C++

To access the address of a variable, we just need to add the Ampersand (&) symbol in front of the variable name. It returns the address of the variable

#include<stdio.h>
main() {
   int x = 59;
   int *p;
   p=&x;
   printf("%d\n", p);
   printf("%d\n", &x);
}

OUTPUT

6487572
6487572

Printing the address of a variable

The address of a variable is a hexadecimal value assigned to it, it actually represents its location in the memory. Often, we use the ' %p ' specifier to print the address in pointer format or pointer value just by printing some leading zeroes. We can also use ' %x ' to print the address in hexadecimal format. With ' %d ' or ' %u ' we get the address in form of numbers

#include<stdio.h>
int main()
{
    int a=4;
    int *p;
    p=&a;
    printf("%d\n",p);
    printf("%p\n",p);
    printf("%x\n",p);
    printf("%u\n",p);
    return 0;
}

OUTPUT

6487572
000000000062FE14
62fe14
6487572

Dereferencing a Pointer

Dereferencing is actually accessing or manipulating the value stored at the address held by the pointer variable. It is done by simply adding * ahead with the variable name. It is also known as value at address operator.

Example:

int *p = &x;
printf("%d",*p);


#include<stdio.h>
int main()
{
    int a=4;
    int *p;
    p=&a;
    printf("%d\n",p);    //prints the address of a
    printf("%d\n",&a);   //prints the address of a
    printf("%d\n",*p);   //prints the value of a
    printf("%d\n",a);    //prints the value of a
    
    return 0;
}

OUTPUT:

6487572
6487572
4
4
 

Example program on pointers

Program:

#include <stdio.h>
int main()
{
  int *ptr;
  int i = 50, j = 10;
  ptr = &i;        /* address of i is assigned to ptr */
  printf("value of i=%d address of i=%p value of p=%p \n", i, &i, ptr);
  printf("value of i=%d Value of variable pointed by ptr=%d\n", i, *ptr);
  ptr = &j;        /* address of i is assigned to ptr */
  printf("value of j=%d address of j=%p value of p=%p \n", j, &j, ptr);
  printf("value of j=%d Value of variable pointed by ptr=%d\n", j, *ptr);
  return 0;
}

OUTPUT

value of i=50 address of i=0xbfa0ecb4 value of p=0xbfa0ecb4 
value of i=50 Value of variable pointed by ptr=50
value of j=10 address of j=0xbfa0ecb8 value of p=0xbfa0ecb8 
value of j=10 Value of variable pointed by ptr=10

Pointer to pointer in C



As we know a pointer points to a memory address. Similarly, a pointer to a pointer points towards the memory address of another pointer, which points to the memory location at which the value is stored.
These are also known as Double pointers

> Declaring a pointer to pointer in C/C++

int **pointer;

It points to a pointer containing the address of another variable. 

Example: 

#include <stdio.h>
int main () 
{
   int  num;
   int  *p;
   int  **pp;

   num = 3;
   p = &num;
   /* pp points to the address of p */
   pp = &p;

   printf("%d\n", num );
   printf("%d\n", *p );
   printf("%d\n", **pp);
   return 0;
}

OUTPUT

3
3
3

We can create a chain of such pointers pointing to each other's addresses. 

Advantages of using Pointers in C

  • Helpful in creating dynamic data structures.
  • Helps indirectly accessing the memory location in the computer's memory
  • Allows passing of strings and arrays to functions more efficiently
  • Reduces execution time in programs
  • Reduces space complexity

Disadvantages of using pointers in C

  • Segmentation fault in case of uninitialized pointers
  • They are slower than normally used variables
  • Memory leak; if memory not freed
  • Chances of memory corruption, if pointers are handled without complete knowledge

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post