C Programming

Pointers in C

Pointers are the most important topic in the C programming language. In memory allocation, each block where any data is stored is capable of storing 1 byte of information. The pointer is the variable that enables us to store the address of the variables to which we want to point. The value of pointers itself can be changed to point to different places in memory, which is powerful when we are pointing to an array because we know that the inner array elements are next to each other. So, we can move the pointer from one element to the next element by just moving along in the memory.

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 var=2;
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.

#include <stdio.h>
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.

#include<stdio.h>
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.

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.