A One-Stop Solution for Using Pointers in C

Pointers are like special utilities, which 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 

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

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
Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!

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 Full Stack Web Developer - MEAN Stack Master’s program gives you exactly that and much more. This globally recognized boot camp program covers 20+ in-demand skills and tools which you will learn via an applied learning approach featuring 10 lesson-end, 4 phase-end projects, and a capstone project in 4 industry domains. 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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.