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.