Pointers in C with Examples

The C programming language offers various features and functionalities to its users. And pointers are one of them. Pointers help programmers in better coding. With the help of a pointer, you can work with different memory addresses of different variables. It is one of the most powerful and useful features of the C programming language.

Let me tell you about addresses in C before we start with pointers.

Address in C

Whenever a variable is defined then you can access the memory address of that variable. Suppose, you define a variable named var then if you use &var, it will give the variable var’s address in memory. In C, you can get the memory address of a variable using & symbol.

Example of Address in C

#include<stdio.h>

int main()
{
  int var = 2;
  printf("Value is: %d\n", var);
  printf("Memory address is: %p\n", &var);
}

Output

Value is: 2
Memory address is: 0x7ffcd857e2dc

What is a pointer in C?

The main purpose of a pointer is to get the memory address of the variable which is defined in the program code. Pointers are special variables. It holds the memory address of another variable of the same data type. Without the help of a pointer, you cannot perform tasks such as dynamic memory allocation and many tasks in the C programming language.

Example of pointer in C

int a=5;
int* point = &a; // pointer variable point is pointing to the address of the integer variable a!

How to use pointers in C?

  • First, you should define a pointer variable.
  • Then, assign the address of a variable to that pointer using & symbol. It will return the address of that variable.
  • You can access the value stored in that address by using *(asterisk) symbol.

We associate data type to a pointer because it knows how much bytes of data can it store.

Basic example of pointers in C

#include <stdio.h>
void TechVidvan()
{
  int var = 25;
  int *pt;
  pt = &var;    
  printf("Address is: %p \n",pt);
  printf("Value is: %d \n", *pt);	 
}
int main()
{
  TechVidvan();
}

Output:-

Address is: 0x7ffc9e1fb704
Value is: 25

Declare a pointer in C

In C, you can declare a pointer variable using *(asterisk) symbol.

Syntax:-

int* p;
char* c;

In the above, we have declared a pointer p of type int and also declared a pointer c of type char.

You can also declare a pointer variable like below:-

int* v1,v2;

In the above, we have declared a pointer v1 and a normal variable v2.

Example of how to Declare a pointer in C

#include<stdio.h>

int main()
{
  int val = 10;
  int* point;
  point = &val;
  printf("TechVidvan Tutorial: Declare a pointer in C!\n\n");
  printf("Value is: %d\n", *point);
  printf("Memory address is: %p\n", point);
}

Output

TechVidvan Tutorial: Declare a pointer in C!

Value is: 10
Memory address is: 0x7ffdcf71bb14

In the above example, the address of the value is assigned to the pointer variable named point. And we have used *point to get the value that is stored in that address.

Changing the value in C pointers

In C, we can also change the value pointed by the pointers easily.

Let’s take the above example. From the above example, we can change the value of the integer variable val like below:-

#include<stdio.h>

int main()
{
  int val = 10;
  int* point;
  point = &val;
  val = 9;
  printf("TechVidvan Tutorial: Changing the value pointed by a pointer!\n\n");
  printf("Value is: %d\n", *point);
  printf("Memory address is: %p\n", point);
}

Output:-

TechVidvan Tutorial: Changing the value pointed by a pointer!

Value is: 9
Memory address is: 0x7ffc167b00a4

See, we have changed the value of val variable from 10 to 9. And the memory address is also different.

More About C Pointers

In C, pointers have various useful concepts that a programmer must learn. Following are some important concepts in pointers:-

Pointer Arithmetic Operators

In a pointer, you can use four arithmetic operators such as ++, –, + and – on pointers. With the help of this, you can perform certain arithmetic operations on pointers.

Incrementing a pointer

In C, you can also increment a pointer. You can use an array which will help you increment the pointer easily.

Example of incrementing a pointer

#include <stdio.h>
int main () {
   int value_array[] = {141, 1};
   int *point;
   point = value_array;
   for (int a = 0; a < 2; a++) {
  printf("TechVidvan Tutorial: Incrementing a pointer!\n\n");
  printf("Value of value_array[%d] = %d\n", a, *point);
  printf("Address of value_array[%d] = %p\n", a, point);
  point++; // incrementing the pointer variable!
   }
   return 0;
}

Output:-

TechVidvan Tutorial: Incrementing a pointer!

Value of value_array[0] = 141
Address of value_array[0] = 0x7ffe22d0679c
TechVidvan Tutorial: Incrementing a pointer!

Value of value_array[1] = 1
Address of value_array[1] = 0x7ffe22d067a0

Decrementing a pointer in C

The C programming language also has the functionality to decrement a pointer.

Example of Decrementing a C pointer

#include <stdio.h>
int main () {
   int value_array[] = {141, 1032};
   int *point;
   int max=2;
   point = &value_array[max-1];
   for (int a = max; a > 0; a--) {
  printf("TechVidvan Tutorial: Decrementing a pointer!\n\n");
  printf("Value of value_array[%d] = %d\n", a-1, *point);
  printf("Address of value_array[%d] = %p\n", a-1, point);
  point--; // decrementing the pointer variable!
   }
   return 0;
}

Output:-

TechVidvan Tutorial: Decrementing a pointer!

Value of value_array[1] = 1032
Address of value_array[1] = 0x7ffeafc6efac
TechVidvan Tutorial: Decrementing a pointer!

Value of value_array[0] = 141
Address of value_array[0] = 0x7ffeafc6efa8

Pointer Comparison in C

You can also compare pointers by using relational operators like ==, <, and >.

Example of C Pointer Comparison

#include <stdio.h>
int main () {
   int value_array[] = {141, 1032};
   int *point;
   int max=2;
   int a=0;
   point = value_array;
   while(point <= &value_array[max-1]) {
  printf("TechVidvan Tutorial: Pointer Comparison!\n\n");
  printf("Value of value_array[%d] = %d\n", a, *point);
  printf("Address of value_array[%d] = %p\n", a, point);
  point++;
  a++;
   }
   return 0;
}

Output:-

TechVidvan Tutorial: Pointer Comparison!

Value of value_array[0] = 141
Address of value_array[0] = 0x7fff9f245d68
TechVidvan Tutorial: Pointer Comparison!

Value of value_array[1] = 1032
Address of value_array[1] = 0x7fff9f245d6c

In the above example, we are incrementing the pointer variable until the address which the pointer points to is less than or equal to the address of the last element of the array.

Array of Pointers in C

In C, we can use an array of pointers to make your coding simple and easy.

For Example:-

int *point[4];

In the above example, we have declared point as an array of 4 integer pointers. And each element in the point variable holds a pointer to an integer value.

Example ofArray of pointers in C

#include <stdio.h>
int main () {

   int  value_array[] = {42, 366, 458, 112};
   int a, *point[4];
   printf("TechVidvan Tutorial: Array of pointers!\n\n");
 
   for (a = 0; a < 4; a++) {
  	point[a] = &value_array[a]; // assigning address!
   }
   for (a= 0; a < 4; a++) {
  	printf("Value => var[%d]: %d\n", a, *point[a] );
   }
   
   return 0;
}

Output:-

TechVidvan Tutorial: Array of pointers!

Value => var[0]: 42
Value => var[1]: 366
Value => var[2]: 458
Value => var[3]: 112

In the above example, we used an array of integers but you can use an array of characters also.

Pointer to Pointer in C

Pointer to pointer is known as a chain of pointers. It means that the first pointer contains the address of the second pointer and it points to the location of actual value.

Syntax:-

int **point;

In the above example, we declared a pointer to pointer of type integer.

Example of Pointer to pointer

#include <stdio.h>
int main () {
   int  val;
   int  *pt;
   int  **point;

   val = 55;
   pt = &val;
   point = &pt;
   printf("Value => val: %d\n", val);
   printf("Value => *pt: %d\n", *pt );
   printf("Value => **pptr: %d\n", **point);
   return 0;
}

Output:-

Value => val: 55
Value => *pt: 55
Value => **pptr: 55

Passing pointers to function in C

You can also pass a pointer to a function. You can just simply declare the function parameter as a pointer type.

Example of Passing pointers to function

#include <stdio.h>
void display(int *val);
int main () {

   int a;
   display(&a);
   printf("TechVidvan Tutorial: Passing pointers to function!\n\n");
   printf("Value is: %d\n",a);
   return 0;
}

void display(int *val) {
   *val = 1002;
  return;
}

Output:-

TechVidvan Tutorial: Passing pointers to function!

Value is: 1002

Return pointers from function in C

In C, you have seen that you can return an array from a function. In a similar way, you can also return a pointer from a function.

NOTE:- If you try to return the address of a local variable outside the function then the local variables of the function will go out of scope. And your program will never get executed.

So to avoid this type of error, you have to define the local variable as a static variable.

Example:- Return a pointer from a function

#include <stdio.h>
int* printToscreen()
{
  static int val = 2364; // defining the local variable as static to avoid execution error!
  return (&val); // function returning pointer!
}
int main()
{
  int* point;
  point = printToscreen();
  printf("TechVidvan Tutorial: Returning pointer from a function!\n\n");
  printf("Address is: %p\n", point);
  printf("Value is: %d\n", *point);
  return 0;
}

Output:-

TechVidvan Tutorial: Returning pointer from a function!

Address is: 0x601048
Value is: 2364

Invalid Pointers in C

A pointer is known as a invalid pointer when it does not point to valid elements. You can also state that Uninitialized pointers are also known as invalid pointers.

int *a;
int ar[10];
int *point = ar+20;

In the above example, *a pointer is uninitialized so it is an invalid pointer. And the pointer variable point is exceeding the limit of array ar. So, it is also an invalid pointer.

Null Pointers in C

A pointer that points to nowhere then it is known as null pointers. There are two methods to assign a null pointer:-

int *point1 = 0;
int *point2 = NULL;

Benefits of using pointers in C

  • Pointers help you to reduce the length and the execution of the program.
  • Using a pointer, you can perform dynamic memory allocation easily.
  • With pointers, it is easy to handle and implement arrays and structures.

Pointer to Structure in C

As you have seen, you can use an array to structure. Similarly, you can use a pointer to the structure. If you use pointers to a structure then you will have to use -> operator to access the members of the structure.

Example of structure pointer in C

#include<stdio.h>
struct point
{
   int a;
   float b;
};
int main()
{
   struct point p1={3,6.3};
   struct point *p2=&p1;
   printf("TechVidvan Tutorial: Pointer to structures!\n\n");
   printf("First value is: %d\n",p2->a);
   printf("Second value is: %0.1f",p2->b);
   return 0;
}

Output:-

TechVidvan Tutorial: Pointer to structures!

First value is: 3
Second value is: 6.3

Usage of Pointers in C

There are many uses of pointers in C programming.

  • Dynamic Memory Allocation:- With the help of pointers, we can easily allocate memory dynamically using calloc() and malloc() functions in C.
  • Arrays, Functions and Structures:- With pointers, it is easy to handle and implement arrays and structures. It helps in reducing code complexity and the execution time.

Complex Pointers in C

You must know that there are some complex pointers available in C. Below is a table of the precedence and associativity of the operators which are used regarding pointers.

Operator Precedence Associativity
(), [] 1 Left to Right
*, identifier  2 Right to Left
Data Type 3 _
  • ():- Used to declare and define a function.
  • []:- Used to represent an array.
  • *:- A pointer operator.
  • Identifier:- Name of the pointer.
  • Data Type:- It is the type of the variable to which the pointer points to.

Read the pointer in C

Before starting to read a pointer, you have to check that if () and [] have the equal precedence. And also look for the associativity.

Example:-

int (*point)[2]

In the above example, you will notice that the associativity is left to right. In that case, the priority goes to (). Inside the bracket, you will notice that *(pointer operator) and point(Identifier) have the same precedence. So, the associativity will go right to left. First priority goes to point and second priority goes to *. At last, you can say that point is a pointer to an array of integers of size 2.

Summary

From the above tutorial, we learnt about pointers. We also discussed some important topics and concepts such as Address in C, Pointer to Pointer, Array to pointer, Passing pointer to a function, Applications of pointer etc. Programmers will gain various benefits by using pointers in C.