Pointers are like special utilities used by web developers to make it easy to map around in a program code. Unlike other variables, pointers store THE addresses of other variables.

What Are Pointers in C?

A pointer is a variable pointing to the address of another variable. It is declared along with an asterisk symbol (*). The syntax to declare a pointer is as follows:

         datatype *var1

The syntax to assign the address of a variable to a pointer is:

         datatype var1, *var2;

var2=&var1;

pointers_in_c-what-are-pointers-img1

In How Many Ways Can You Access Variables?

You can access a variable in two ways.

  • Direct Access: You can directly use the variable name to access the variable.
  • Indirect Access: You use a pointer to access that variable.

pointers_in_c-variable-access-img2

Example:

#include<stdio.h>

int main()

{

  int a=5, *ptr;

  ptr=&a;

  printf(“Direct Access, a=%d\n”,a);

printf(“Indirect Access, a=%d\n”,*ptr);

  return 0;

}

pointers_in_c-variable-access-img2

What Are the Different Types of Pointers?

There are majorly four types of pointers, they are:

  • Null Pointer
  • Void Pointer
  • Wild Pointer
  • Dangling Pointer 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Null Pointer: 

pointers_in_c-null-pointer-img1

If you assign a NULL value to a pointer during its declaration, it is called Null Pointer.

Syntax:

Int *var = NULL;

Example:

#include<stdio.h>

int main()

{

int *var = NULL;

printf(“var=%d”,*var);

}

pointers_in_c-null-pointer-img2.

Void Pointer:

pointers_in_c-void-pointer-img1.

When a pointer is declared with a void keyword, then it is called a void pointer. To print the value of this pointer, you need to typecast it.

Syntax:

void *var;

Example:

#include<stdio.h>

int main()

{

     int a=2;

     void *ptr;

ptr= &a;

printf("After Typecasting, a = %d", *(int *)ptr);

     return 0;

}

pointers_in_c-void-pointer-img2.

Wild Pointer:

pointers_in_c-wild-pointer-img1

A wild pointer is only declared but not assigned an address of any variable. They are very tricky, and they may cause segmentation errors.

Example:

#include<stdio.h>

int main()

{

  int *ptr;

  printf(“ptr=%d”,*ptr);

  return 0;

}

pointers_in_c-wild-pointer-img2

Dangling Pointer

pointers_in_c-dangling-pointer-img1

  • Suppose there is a pointer p pointing at a variable at memory 1004. If you deallocate this memory, then this p is called a dangling pointer.
  • You can deallocate a memory using a free() function.

Example:

#include<stdio.h>

#include<stdlib.h>

int main()

{

  int *ptr=(int *)malloc(sizeof(int));

  int a=5;

  ptr=&a;

  free(ptr);

  //now this ptr is known as dangling pointer.

printf(“After deallocating its memory *ptr=%d”,*ptr);

  return 0;

}

pointers_in_c-dangling-pointer-img2

So far, you have learned about the types of pointers. Now, you will look into more practical aspects. Let's discuss some use cases of pointers in C.

What Are the Use Cases of Pointers in C?

  • Pointer arithmetic
  • Pointer to pointer
  •  Array of pointers
  •  Call by value
  • Call by reference

Pointer arithmetic:

  • Increment: You can use this operator to jump from one index to the next index in an array.

Syntax:

           ptr++;

pointers_in_c-pointer-arithmetic-increment-img1

Example:

#include <stdio.h>

int main() {

  int arr[3] = {50, 150, 200};

  int *ptr;

  ptr = arr;

  for (int i = 0; i < 3; i++)

  {

       printf(“Value of *ptr = %d\n”,*ptr);

       printf(“Address of *ptr = %d\n”,ptr);

       ptr++; 

  } 

pointers_in_c-pointer-arithmetic-increment-img2

  • Decrement: You can use this operator to jump from one index to the previous index in an array.

Syntax:

              Ptr--;

arithmetic-decrement-img

Example:

#include<stdio.h>

int main()

{

int arr[3]={50, 150, 200};

int *ptr;

ptr = &arr[2];

for (int i=0;i<3;i++)

{

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr--; 

}

}

arithmetic-decrement-img2

  • Integers added to a Pointer: You can use this operator to jump from one index to the next ith index in an array.

Syntax:

ptr+=i; // where ‘i’ is an integer

pointers_in_c-pointer-arithmetic-addition-img1

Example:

                 #include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};

int *ptr;

ptr = &arr[0];

for (int i = 0; i < 5; i++) {

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr=ptr+2;

}

}

pointers_in_c-pointer-arithmetic-addition-img2

  • Integers Subtracted from a Pointer: You can use this operator to jump from one index to the previous ith index in an array.

Syntax:

              ptr-=i; // where ‘i’ is an integer

pointers_in_c-pointer-arithmetic-subtraction-img1

Example:

#include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};

int *ptr;

ptr = &arr[4];

for (int i = 0; i<5; i++)

{

printf("Value of *ptr = %d\n", *ptr);

printf("address of *ptr = %d\n\n", ptr);

ptr-=2; 

}

}

pointers_in_c-pointer-arithmetic-subtraction-img2

  • Precedence:
  • Operators * and & are given the same priorities as unary operators (increment++, decrement--).
  • The unary operators *, &, ++, - are evaluated from right to left in the same expression.
  • If a P points to an X variable, then you can interchange X with *P.

Expression

Equivalent Expression

Y=X+1

Y=*P+1

X=X+10

*P=*P+10

X+=2

*P+=2

++X

++*P

X++

(*P)++

Pointer to Pointer: 

pointers_in_c-pointer-to-pointer-img1

In this situation, a pointer will indirectly point to a variable via another pointer.

Syntax:

Int **ptr;

Example:

#include <stdio.h>

int main ()

{

int var, *ptr1, **ptr2;

var = 10;

ptr1 = &var;

ptr2 = &ptr1;

printf("Value of var = %d\n", var );

printf("Value available at *ptr1 = %d\n", *ptr1 );

printf("Value available at **ptr2 = %d\n", **ptr2);

return 0;

}

pointers_in_c-pointer-to-pointer-img2

An Array of Pointer: 

An array of the pointer is an array whose every element is a pointer.

Syntax:

int *arr[n] //where n is size of array.

pointers_in_c-Array-of-pointer-img1

Example:

#include <stdio.h>

int main ()

{

int a[3] = {10, 100, 200},n=3;

int i, *ptr[3];

for ( i = 0; i < 3; i++)

{

  ptr[i] = &a[i];

}

for ( i = 0; i < n; i++)

{

  printf("Value of a[%d] = %d\n", i, *ptr[i] );

}

return 0;

}

pointers_in_c-Array-of-pointer-img2

Call By Value: 

In ‘call by value’, you must copy the variable's values and pass them in the function call as a parameter. If you modify these parameters, then it doesn't change the value of the actual variable. 

pointers_in_c-call-by-value-img1

Example:

#include<stdio.h>  

void change(int num)

{

printf("Before adding value inside function num=%d\n",num);

num=num+100;

printf("After adding value inside function num=%d\n",num);

}

int main()

{

int x=100;

printf("Before function call x=%d \n",x);

change(x);

printf("After function call x=%d \n",x);

return 0;

}

pointers_in_c-call-by-value-img2.

Call By Reference: 

In call by reference, you must take the variable's address and pass it in the function call as a parameter. If you modify these parameters, then it will change the value of the actual variable as well. 

pointers_in_c-call-by-reference-img1

Example:

#include<stdio.h>

void change(int *num)

{

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n",*num);

}

int main()

{

  int x=100;

  printf("Before function call x=%d \n",x);

  change(&x);//passing reference in function    

  printf("After function call x=%d \n",x);

  return 0;

}

pointers_in_c-call-by-reference-img2

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Are the Advantages of Pointers in C?

pointers_in_c-advantages-img1

  • Pointers in C programming are helpful to access a memory location
  • Pointers are an effective way to access the array structure elements
  • Pointers are used for the allocation of dynamic memory and the distribution
  • Pointers are used to build complicated data structures like a linked list, graph, tree, etc

What Are the Disadvantages of Pointers?

pointers_in_c-disadvantages-img1

  • Pointers are a bit difficult to understand
  • Pointers can cause several errors, such as segmentation errors or unrequired memory access
  • If a pointer has an incorrect value, it may corrupt the memory
  • Pointers may also cause memory leakage
  • The pointers are relatively slower than the variables

What Are the Key Takeaways?

  • The Pointers in C programming is simply a storage location for data in memory
  • Pointers can be used to traverse the array more efficiently
  • You can use function pointers to invoke a function dynamically
  • Pointer arithmetic is the process of performing arithmetic operations on a pointer
  • In an array of pointers, pointers can point to functions, making it simple to call different functions

Next Steps

"Arrays in Data structures" can be your next stop. Arrays in data structures can be helpful in competitive programming and other data structure implementations. Arrays allocate memory in contiguous memory locations for all its elements making it easier to access elements. 

Are you looking to learn further and master today’s top programming languages? Well, Simplilearn’s Post Graduate Program In Full Stack Web Development program gives you exactly that and much more. This globally recognized program will help you learn modern coding techniques with bootcamp-level intensity and gain all you need to be a full-stack technologist. Do explore the course today.

If you have any questions about “Pointers in C”, please feel free to leave them in the comments section below. Our 24/7 expert team will answer all your queries for you at the earliest!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 18 Dec, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449