Dereference pointer in C

Dereferencing is a method used to manipulate or access data contained in a memory location that it is pointed to by a pointer. Asterisk (*) is used along with a pointer variable to dereference a pointer variable; it refers to a variable that is being pointed. Hence it is called dereferencing of a pointer. Dereferencing operator in the C programming is also known as indirection pointer, and as we know, it operates only on pointer variable. It returns the location value or, as you call l-value, present in the memory pointed to by the variable’s value. When an indirection pointer is used along with a pointer, it is known as dereferencing the pointer. It is used when any operations are applied to a dereferenced pointer that will directly affect the value of the variable that the pointer points to.

            E.g:

In the above example, the address in p is the address of a variable. The “int *p” defines a pointer to an integer, and *p would dereference that pointer. That is, it will retrieve the data that ‘p’ points to.

E.g:

Asterisk (*) lets the compiler know that “p is not an integer, yet rather a pointer to a location in the memory which holds an integer.” It is a part of the pointer declaration. Pointer is a variable that stores the address of another variable. Set ‘p’ to the location allocated for the value of ‘x’ using the ‘&’ operator, which indicates “address of.”

Suppose a program contains a variable that is a pointer to a structure with a data member or data members. In that case, you can easily access the members by using dereferencing operator (->).

Steps on how to dereference a pointer:

  1. Declare the integer variable to which a pointer points:

int a = 3;

  • Declare the integer pointer variable required:

int *ptr;

  • After declaring an integer pointer variable, we can store the address of variable ‘a’ to a pointer variable ‘ptr’:

ptr = &a;

  • Later the value of ‘a’ can be changed by dereferencing a pointer ‘ptr.’

*ptr = 5;

If you follow the above-mentioned steps, it can be seen that the value of the variable ‘a’ will be changed to 5. This is because the pointer ‘ptr’ points to the variable ‘a’ location, and dereferencing the pointer ‘ptr,’ that is, *ptr = 5, will update the value of x.

Consider an instance:

Output:

The value of variable a is: 21

Consider another instance that allocated memory dynamically.

Dereference operation begins at the pointer, and it then follows the arrow over to access its pointee, that is, the value it is referencing to. The goal may be to look at the pointee or to change the pointee state. The dereference operation on a pointer only works if the pointer has a pointee; else, it will be terminated and show an error.

The pointee must be allocated, and then the pointer must be set to point the pointee. Sometimes the pointer code might forget to set up pointee; this is a frequent error that misleads a programmer most often and creates an error.

The expected runtime crash because of this error in the code will fail the dereference operation. In compiled programming languages such as C, C++, Java, Pascal, and so on, the incorrect usage of dereferencing will cause an application crash. Most of the time, it might corrupt memory in some or another way. Pointer bugs in compiled languages can be challenging to track down for this very reason and should be dealt with very carefully.

Pin It on Pinterest

Share This