Full Stack Developer
The Pointer in C is a variable that stores the address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of a pointer is to save memory space and achieve faster execution time.
Let us try to know what are the different ways by which we can print the address of a variable in C.
There are two ways by which we can actually print the address of a variable. Those two methods are:
Let us look into each method with an example
In this method, we will be using the concept of the ampersand to print the address of a variable.
Let us look this with an example
#include <stdio.h>
int main () {
double varNumValue= 10.2;
char varTextValue[10] = “abc”;
printf("The address of variable varNumValue: %x\n", &varNumValue );
printf("varTextValue variable address is : %x\n", &varTextValue );
return 0;
}
Now, copy the above code snippet and run it
It will show the following output:
The address of variable varNumValue: a0665a48
The address of variable varTextValue is : a0665a3e
Although, one might see the different value which is assigned randomly while running the example
The above-mentioned example is not much used, but it is worth knowing. Now let us look to the other concept which we are discussing in this topic.
This is a modern approach to access the address of a variable. Before using a pointer, let us know the general syntax of declaring a pointer.
data_type *variable
This is a common way to declare any pointer in C, here data_type represents the type of the variable whose address needs to be stored. * denotes that the variable declared is a pointer. a variable is simply used for accessing the value
A simple example of this is:
double *var_text
After discussing the concept of the General syntax of declaring a pointer, let us know how we can use a pointer in programming.
To use a pointer in C, basically, one needs to follow the following three steps:
Let us use the above-mentioned steps with an example, and then we will explain this example step by step.
Example
#include <stdio.h>
int main () {
int varNumValue = 10;
int *ipointervarNumValue;
ipointervarNumValue = &varNumValue;
printf("Address of the variable varNumValue is: %x\n", &varNumValue );
printf("Address stored in the variable ipointervarNumValue is: %x\n", ipointervarNumValue);
printf("Value of the variable *ipointervarNumValue is: %d\n", *ipointervarNumValue );
return 0;
}
Now, copy the above code snippet and run it
It will show the following output:
Address of the variable varNumValue is: bf7b7f94
Address stored in the variable ipointervarNumValue is: bf7b7f94
Value of the variable *ipointervarNumValue is: 10
Now, let us try to understand the above example
The line int varNumValue = 10; simply declares a variable with value 10
The second line that is a line next to it: int * ipointervarNumValue; is simply declared to store the address of the variable
The next code snippet, which is: ipointervarNumValue = &varNumValue, is used to hold the address of the variable. Here, we are using the concept of an ampersand, which we discussed earlier in the article
Now, we can simply get the value and use a pointer in our code snippet
Now, let us look into different types of a pointer in C
Different types of pointers in C are following:
We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output:
The value inside variable p is:
0
In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.
Following program illustrates the use of a void pointer:
#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Output:
The size of pointer is:4
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Output:
timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault timeout 10s main
Pointers in C are used to point to the address of the variable. These variables are used for the dynamic allocation of memory in C. These variables are declared with an asterisk so as to show that the variable is a pointer. These are used in the header file in programming.