Pointers in C:

When we declare a variable, it is stored in the memory at some address. Pointers are variables that store memory addresses of the other variables. They allow us to manipulate data indirectly by referring to the memory location where the data is stored. Pointers store data in hexadecimal form.


First we will see how can we get the address of a variable with the help of "&" operator. We store the address in pointer variable.

pointers.c
#include <stdio.h>

int main() {
  int num = 20;
    
  printf("%d\n", num);
  printf("%p\n", &age);
  return 0;
}

Declaring Pointers:

We declare pointers using * after the data type of the variable to which pointer is pointing. It is declared like a simple variable but we put * after the data type of the variable to which pointer is pointing.

pointers.c
#include <stdio.h>

int main() {
  int num = 22;  
  int* numPtr = &age;  
  
  printf("%d\n", num);
  printf("%p\n", numPtr);
  
  return 0;
}

There are two ways to declare a pointer variable;

  • int* ptr;
  • int *ptr;


Dereferencing:

To access the value pointed to by a pointer, we use the asterisk (*) symbol.

pointers.c
#include <stdio.h>

int main() {
  int num = 22;  
  int* numPtr = &age;  
  
  printf("%d\n", *agePtr);
  
  return 0;
}

Data type of Pointer:

Data type of pointers refers to the data type of variable to which pointer is pointing.
  • int *ptr means ptr is pointing to int type variable.
  • float *ptr means ptr is pointing to float type data.

If we do not want to specify the data type of pointer or if we do not know what kind of data pointer is pointing to, we can set the data type of pointer "void".


Pointers to pointers:

Pointers can also point to other pointers. These are called "double pointers" or "pointers to pointers."

pointers.c
#include <stdio.h>

int main() {
  int age = 42;
  int *agePtr = &age; ptr
  int **doublePtr = &agePtr; 
    
  printf("%p\n", &age);
    
  // printing address of age
  printf("%p\n", agePtr);
   
  // printing address of agePtr
  printf("%p\n", *doublePtr);

  return 0;
}

Pointers and Arrays:

Pointers and arrays in C are closely related concepts. In fact, in C, arrays are implemented as a contiguous block of memory, and the array name acts as a pointer to the first element of the array.

pointers.c
#include <stdio.h>
 
int main() {
  int ages[5] = {37, 60, 55, 95, 78};
  int *agePtr = ages;

  for (int j = 0; j < 5; j++) {
    printf("%p\n", (agePtr + j));
  }
  return 0;
}

In this example;


  • Initializing an int type array of size 5.
  • Initializing a pointer to array. marks atore the base address of array. It points to the first element of the array (90).
  • Loop starts and ptr has address of array[0]. i = 0. So, it will point to 90.
  • In the next iteration, i = 1. So it will print ptr + 1 which is 80.
  • In the next iteration, i = 2. So it will print ptr + 2 which is 88.
  • In the next iteration, i = 3. So it will print ptr + 3 which is 95.
  • In the next iteration, i = 4. So it will print ptr + 4 which is 78.
  • In the next iteration, i = 5. condition becomes false. The control will move out of the loop.

Importance of Pointers:

  • Pointers allow you to allocate memory dynamically at runtime, which is essential for tasks like creating data structures (e.g., linked lists, trees, and dynamic arrays) and working with variable-sized data.
  • C is mainly used to build system-level and embedded applications that interact closely with the operating systems. Pointers enable interaction with system calls and kernel-level programming.
  • Unlike Java and some C++ libraries, C does not have automatic garbage collection.
  • Pointers in C can be used to directly access memory addresses, making it possible to interface with hardware, memory-mapped I/O, and perform low-level system programming.