Welcome back guys, in this module, we are going to discuss what are pointers in C Programming, why we need pointers, what are its advantages and disadvantages, their uses, etc.
So, let’s dive into this module.
What are Pointers in C Programming?
Pointers in C Programming 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 various tasks which can’t be executed using simple programming such as dynamic memory allocation. We can’t perform this without using a pointer.
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 Pointers in C Programming 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("The Address of integer variable is: %x\n", &num1 ); // accessing the address of char variable printf("The Address of character variable is: %x\n", &ch ); return 0; }
The output of the above Pointers in C Programming is:
The address of integer variable is: 4e6d292c The 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 accessed their addresses by using an ampersand operator and got the resultant output.
In simple terms, we can say that Pointers in C Programming are variables, 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 in C Programming?
The general syntax of declaring the Pointers in C Programming 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 Pointers in C Programming 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 in C Programming?
Let us understand this by taking the example of Pointers in C Programming.
int *p ; int c = 5 ; p = &c ;
In the above Pointers in C Programming 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 printed the address of the variable and the value stored in that pointer variable.
What is a NULL pointer in C Programming?
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 through the following program in C Programming.
#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 Programming?
Let’s see some of the advantages of using pointers in C programming.
- 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 programming?
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 the 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 Pointers in C Programming, and you all must be excited about the upcoming wonderful and exciting topics of this series.
So, until then stay connected, Happy Learning, Keep practising, and Keep coding!