What are Pointers in C Programming?

Welcome back guys, in this module, we are going to discuss what are pointers in C Programming, why do we need pointers, what are its advantages and disadvantages, its uses, etc.

So, let’s dive into this module.

What are Pointers in C Programming?

Pointers in C are used to store the address of a particular variable, instead of storing values. It becomes very easy to analyze the program while using pointers. It can easily handle the various task which can’t be executed using simple programming such as dynamic memory allocation. We can’t perform this without using a pointer.

What are Pointers in C Programming

We know that each and every variable is a memory location and that memory location has its own address defined. So, we can access that address by using an ampersand (&) operator. This operator denotes the address in the memory.

Let’s understand this by taking a simple example.

#include <stdio.h>
int main ( ) {
 int num1;      // integer variable declared
 char ch[5];     // character variable declared 
   // accessing the address of int variable 
   printf("Address of integer variable is: %x\n", &num1 ); 
   // accessing the address of char variable                  
   printf("Address of character variable is: %x\n", &ch );         
 return 0;
}

The output of the above program is:

Address of integer variable is: 4e6d292c
Address of character variable is: 4e6d2933

In the above program, we have simply declared 2 variables of integer and character data type respectively, and then have accessed their addresses by using an ampersand operator and got the resultant output.

In simple terms, we can say that pointers are the variable, whose value signifies the address of another variable, i.e., you can assign the direct address of the memory location to some variable.

How to declare a pointer variable?

The general syntax of declaring the pointer in C is:
data_type *var_name ;

Here, in this syntax data_type represents the base type of the pointer, it must be a relevant c data type. var_name is the pointer variable name, asterisk representing that the pointer variable has been declared.

Some of the valid declarations of the pointer are:

int *ip ;    // pointer to an integer declaration
double *d ;    // pointer to a double declaration
float *f ;    // pointer to a float declaration
char *c ;    // pointer to a character declaration

How to assign the addresses to the pointers?

Let us understand this by taking an example.

int *p ;
int c = 5 ;
p = &c ;

In the above example, we have declared a pointer to integer variable p, and a normal variable c, in which we have assigned the value 5, and then assigned the address of the variable c to the pointer p. Let’s see the program to understand more clearly.

#include <stdio.h>
int main ( ) { 
   int  c = 5;  // actual variable declaration 
   int  *p;   // pointer to an integer variable declaration
   p = &c;  // store address of var in pointer variable

   printf("Address of the variable is: %x\n", &c );

   // address stored in pointer variable 
   printf("Address stored in pointer is: %x\n", p );

   // access the value using the pointer 
   printf("Value stored in pointer is: %d\n", *p );
return 0;
}

The output of the above program is:

Address of the variable is: 489371f4
Address stored in pointer variable is: 489371f4
Value stored in pointer variable is: 5

In the above program, we have declared a pointer, assign it to some value, and then printing the address of the variable and the value stored in that pointer variable.

What is a NULL pointer in C?

If you don’t have any address to assign to a pointer variable, then in that case you can assign a NULL value. It can be done at the time of declaration of the pointer variable. It is a good practice to use a NULL value and the pointer which is assigned a NULL value is called a NULL pointer.

Consider this by the following program.

#include <stdio.h>
int main () {
   int  *p = NULL;   // pointer variable assigned to NULL
   printf(“Value *p is : %x\n", p  );
   return 0;
}

The output of the above program is:

Value *p is : 0

In the above program, we have assigned a NULL to the pointer variable and then got the value of the pointer to be 0 as output.

What are the advantages of pointers in C?

Let’s see some of the advantages of using pointers in C.

  • It reduces the complexity of the program.
  • It helps in reducing the storage space of the program.
  • It reduces the program execution time.
  • It allows us to resize the memory dynamically.
  • Through pointers, you have direct access to memory.
  • It allows us to perform memory allocation and deallocation dynamically.
  • It allows us to access the address of the particular variable.
  • We can handle structures and arrays in a more efficient way by using pointers.
  • We can’t able to work in complex data structures such as linked lists, stacks, etc, without using pointers.

What are the disadvantages of pointers in C?

Well apart from having plenty of advantages, it has some disadvantages or drawbacks also. Let’s see some of them.

  • It is very slow as compared to normal variables.
  • It may lead to corruption of memory if it is updated with wrong or incorrect values.
  • It may lead to a memory leak because dynamically allocated blocks need to be explicitly freed.
  • It may cause to segmentation fault if it is not initialized properly.
  • The program may sometimes crash if sufficient memory is not available during the run time for the pointer storage.

I hope, this module helped you all a lot in understanding this wonderful concept of C, and you all must be excited about the upcoming wonderful and exciting topics of this series.

So, until then stay connected, Happy Learning, Keep practicing, Keep coding!