Pointer In C Language – Uses, Advantages, Example of Pointer in C
In C language, the concept of pointer is very important. The use of pointer in C language makes some tasks easy (eg – dynamic memory allocation)
Before knowing about pointer, you have to know about address operator (&) and indirection operator (*). Because both of these things will increase your knowledge about Pointer, so first of all you know about them and then read this article
Now Let’s understand what is pointer In C Language and how to use pointer in C language.
Introduction of Pointer In C
Pointer is a variable that stores or contains the address of another variable. This variable can be of any type int, char, float, double, array, function, or pointer.
The size of a pointer is 4 byte according to 32-bit architecture. The size of the pointer depends on the architecture, if your compiler is 64 bit then the size of the pointer will be 8 byte and if it is a 32-bit compiler then the size of the pointer will be 4 byte.
The data type of a pointer does not affect the size of the pointer, but the pointer points to the address of a variable of the same type. For example, if a pointer is an Integer type, then it will point or store only Integer type variable address.
Declaration of Pointers in C:
The way we declare a variable in C language, we also have to declare a pointer variable. While declaring a pointer variable, the asterisk symbol (*) is used before the variable name.
Syntax of Pointer
data_type *var_name;
Example -:
int *pointer_Name;
Look at the example below, in this, we will create a pointer variable and store the address of another variable.
int x;
int p = &x;
Note –: Address (&) is an operator that provides the address of a variable.
In this example, x is a normal variable and p is a pointer variable which we have created without using the asterisk symbol (*). In pointer variable p we are storing the address of x variable.
Since our program can have many variables, how will we find out which variable is a pointer variable and which variable is a normal variable?
In C language, the asterisk symbol (*) is used to show or identify the difference between a normal variable and a pointer variable.
When we create a pointer variable using asterisk symbol (*). Then we can easily distinguish the pointer variable from the normal variable.
In the above example, we created a pointer variable without using asterisk symbol (*) so it was difficult to identify which is pointer variable and which is normal variable.
Let’s now make a pointer using the asterisk symbol (*), so that we can easily distinguish between a pointer variable and a normal variable.
int x;
int *p = &x;
Initialize a Pointer
As we know pointer variable stores the address of another variable. Contains the address of a variable of the same type as the pointer. To initialize a pointer variable, we must have an already declared variable, only then we will be able to keep the address of that variable in this pointer variable.
Example -:
#include<stdio.h>
void main()
{
int x = 10;
int *p = &x;
printf(" x = %d p = %d",x, p);
}
Output -:
x = 10 , p = 1021817273
In this output 1021817273 is the address of the x variable which we stored in the pointer variable p . Through this address also we can access and use x variable.
Let’s understand what is the reason for using pointer or why and where is the pointer used and in which condition.
Use of Pointer in C
- Pointer is used in dynamic memory allocation and deallocation.
- It is used in complex data structures like linked lists, graphs, tree, etc.
- Pointer is used in system-level programming where memory address is very important.
- To implement the data structure.
- Pointers are used when multiple values are to be returned simultaneously.
- Pointers can also be used to access array elements.
- Pointer is used when we have to pass the address of a variable to the function as a reference.
- Pointer is used in file handling.
Advantages of Pointer in C
Following are the advantages of using a pointer -:
- Pointer saves memory.
- Pointer increases the processing speed because the manipulation of data is done through the address.
- It helps in accessing the array in a very efficient manner.
- Can access memory address very efficiently.
- Since the pointer is used with data structures, it is useful for representing two-dimensional and multi-dimensional arrays.
- Pointer is very useful in file handling.
- Pointer is very useful in dynamic memory allocation. With the help of pointer, we can easily do dynamic memory allocation.
Disadvantages of Pointer in C
There are also some disadvantages of using pointer, let’s understand those disadvantages.
Following are the disadvantages of using a pointer -:
- Understanding pointer is a bit complicated.
- If an implicit value is provided to the pointer, memory corruption can occur.
- Pointers are slightly slower than normal variables.
- There is a possibility of memory leakage.
- Working with pointer may be a bit difficult for the programmer but it is the programmer’s responsibility to use the pointer properly.
- Using a pointer can cause many problems, so it is necessary to use the pointer correctly.
Example of Pointer in C
Let’s understand pointer through an example.
#include <stdio.h>
int main()
{
int var1 =10;
int *ptr;
ptr = &var1;
printf ( "Address of var1 is: %p", &var1);
printf ( "\nAddress of var1 is: %p", ptr);
printf ( "\nValue of var1 is: %d", var1);
printf ( "\nValue of var1 is: %d", *ptr);
printf ( "\nValue of var1 is: %d", *( &var1));
/* Note I have used %p for p's value as it represents an address*/
printf( "\nValue of pointer ptr is: %p", ptr);
printf ( "\nAddress of pointer ptr is: %p", &ptr);
return 0;
}
Output -:
Address of var1 is: 0x7ffe4ad9c2e4
Address of var1 is: 0x7ffe4ad9c2e4
Value of var1 is: 10
Value of var1 is: 10
Value of var1 is: 10
Value of pointer ptr is: 0x7ffe4ad9c2e4
Address of pointer ptr is: 0x7ffe4ad9c2e8
Program Explanation -:
In this example, we have created a normal variable named var1 and a pointer variable named ptr. In the pointer variable ptr, we have kept the address of the variable var1. With the help of this address, we are easily accessing var1 and printing the store value in it. Along with this, we are also printing the address of the pointer variable and the normal variable in the screen by the & operator.
Key point to remember about pointer
- Normal variables store the value while pointer variables store the address of the variable.
- The * symbol is used to represent a pointer variable.
- When we also write the name of the pointer with *, then it points to the variable whose address we have kept in this pointer.
- The type of pointer is that it contains the address of the variable of the same type.
- The size of a pointer variable is always 4 bytes (according to the 32 bit architecture).
- We cannot add, multiply and divide two addresses.
- We cannot multiply and divide integer values with variable address.
- The & symbol is used to get the address of a variable.
Read More -:
- Pointer Arithmetic in C
- Address Operator (&) In C
- Indirection Operator(*) In C
- Pointer to Pointer in C
- Pointer to Structure In C
- Dangling Pointer in C
- Introduction of Preprocessor In C
- Dynamic Memory Allocation in C
- Introduction of File Handling in C
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is pointer In C Language and how to use pointer in C language
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about pointer In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.