Whenever a variable is declared, the memory location is automatically allocated to them by default, which is used to hold the value that is assigned to it. The location that is assigned to the variable has a predefined address that can only be accessed by using the pointers.
Syntax
$ type *variable-name;
The syntax for the pointer declaration is a bit different from declaring a variable because while using pointers, we have both abilities to access the address as well as the value of the variable. In the declaration of pointers, the keyword β*β is used which indicates that we are pointing to the variable βxyzβ. In the given syntax, type is the data type of the pointer variable that we are going to define and the βvariable-nameβ is the variable that is to be pointed.
Letβs suppose we created a variable βVarβ whose value is located in the β3e97b57cβ address.
Int *ptr;
ptr=&var;
Now, defining a pointer variable βptrβ, the pointer is defined using the asterisk β*β operator. The technical word used for this operator is dereferencing. To point to the address of the variable that we have to access, we will use the ampersand β&β keyword.
As shown in the following figure, pointer ptr holds the address of the variable βvarβ.
Example 1:
In this example, we implement our pointer through which we will display the address of the variable as well as the variable value. To implement the pointers, we will first include our header file βstdio.hβ and know that the βstdio.hβ is a header file that enables us to perform the input-output operations in C. Now, moving to our main() function, we use our pointers.
Int main()
{
int var = 10;
int *ptr;
ptr=&var;
printf(βAddress of variable var is : %x\nβ, ptr );
printf(βvalue of variable var is : %x\nβ, *ptr );
return (0):
}
In the main() function, we will first declare a variable named βvarβ and assign it a value of β10β. Now, βvarβ holds a value of β10β. Then, we will declare a pointer of integer type, naming it βptrβ. To assign the address of variable βvarβ to the pointer, we will use the ampersand β&β. The ampersand β&β is called the reference operator that is used to create the pointer or address of the information that is stored in a memory location.
Now, the pointer is responsible for holding the address of the variable βvarβ. In the printf statement, %d denotes decimal values and %x represents that we want the numbers to come out in hexadecimal which is a bit easier to read. We print the address of the variable that is β3e97b57cβ. In the second print statement, we print the value of variable βvarβ using the pointer βptrβ. Now, as we can see in the first statement, we simply call the βptrβ without the asterisk β*β that enables us to print the address of the variable.
As we discussed in the introduction, the pointers hold the address of the information that is stored in the memory. On the other hand, when we display the value of the variable that is stored in our memory, we call the pointer using the asterisk β*β.
As shown in the following screenshot, we can see the output of both print statements. The first one prints the address whereas the second one prints the value of a variable.
Example 2:
In the following code, we will change the value of a variable using pointers. As we know, pointers are the most important part of the C language. By using them, we can reduce the time and complexity of code and allows us to handle the unlimited data.
int main()
{
int myvar = 23;
int *myptr;
myptr = &myvar;
printf("Address of myvar= %p\n", myptr);
printf("Value of myvar= %d \n", *myptr);
*myptr= 10;
printf("Updated value of myvar using pointer is %d", *myptr);
}
After including the header files, we will move into our main function where we initialize the variable named βmyvarβ that is responsible for holding the value β23β. And now, initializing a pointer of type integer that is βmyptrβ points to the address of the variable that we declare in the upper line. Now, in our next step, we assign the address of the variable βmyvarβ that is stored in memory having the value β23β. For doing so, we use the keyword ampersand β&β as we discussed in the previous example that ampersand is used to create the pointer or address to the variable that is stored in a memory.
After assigning the address to the pointer, we call the printf() statement to print the myvar value as well as its address. Now, to update the value at the specified address of the variable, we use a pointer to update its value. For doing so, we call the pointer address using asterisk β*β pointer-name that is βmyptrβ. Then, assign it the value that is β10β in our case. Then, we print the value by passing the updated pointer to the printf() statement.
As displayed in the following snippet, the address of the variable is accessed using a pointer and the value is stored at that address. At last, we display the updated value of that variable.
Conclusion
In this guide, we briefly discussed about the pointers in C language and the methods to implement them. We tried our best to explain the implementation of pointers and how to use them efficiently in C language by defining them using examples. Pointer is helpful to reduce the size of code and is also used to improve the performance of huge programs. You can also use pointers to perform the various tasks like arithmetic operations, updating of data, etc.