What is Pointer Indirection or Dereferencing in C?

Question: What is Indirection or Dereferencing a C Pointer and Does a Pointer have In-Built Property of Indirection?

Answer: A pointer variable is like an ordinary variable in that it is allocated location, by compiler, in memory when declared in the program. But unlike ordinary variable, a pointer holds address. Since memory is Byte addressable, every byte has address. Address itself is a constant value. Pointer holds addresses, meaning that what address does it hold, it points to that location in memory. For example:

/* add_to_ptr.c -- Program shows how pointer is assigned an address */
#include <stdio.h>
 
int main(void)
{
    int balls = 100;
    int *ip = &balls;
    /* ip is declared and initialized with address of integer balls */
 
    printf("Address of balls in exp. \"balls = 100\" is %p\nand Value of ip"
           " in exp. \"*ip = &balls\" is %p\n", &balls, ip);
 
    return 0;
}

Output of the above program is as follows:

Address of balls in exp. "balls = 100" is 0x7fffb6801824
and Value of ip in exp. "*ip = &balls" is 0x7fffb6801824

Observe here that both addresses are same. Value of pointer variable, which is address of integer balls, points to the location allocated to integer balls. But how to access the value at that location. In simple ways, integer ‘balls’ gives value at that location. For example:

advertisement
advertisement
/* 
 * deref_ptr.c -- Program shows how pointer is dereferenced or performed
 * upon indirection
 */
#include <stdio.h>
 
int main(void)
{
    int balls = 100;
    int *ip = &balls; 
    /* "ip" is declared and initialized with address of integer balls */
 
    printf("\nAddress of balls in exp. \"balls = 100\" is %p\nand Value of "
           "ip in exp. \"*ip = &balls\" is %p\n\n", &balls, ip);
 
    printf("Value of integer balls in exp. \"balls = 100\" is %d\n", balls);
    printf("\nAccessing the value of balls indirectly using\ninteger "
           "pointer ip in the exp. \"*ip = &balls\" is %d\n\n", *ip);
 
    return 0;
}

Output of the above program is given below:

Address of balls in exp. "balls = 100" is 0x7ffffb7f6ce4
and Value of ip in exp. "*ip = &balls" is 0x7ffffb7f6ce4
 
Value of integer balls in exp. "balls = 100" is 100
 
Accessing the value of integer balls indirectly using
integer pointer ip in the exp. "*ip = &balls" is 100

Notice in the last printf() statement, we accessed the value of the integer balls indirectly using integer pointer ip. Since we have indirectly accessed balls, we perhaps call this Indirection or Dereferencing the pointer and unary operator (*) the Indirection operator. Pointer doesn’t have built-in property of Indirection. Preceding a pointer with unary (*) operator, like *ip; in the exp. *ip = &balls, is read as “go to the location pointed to by pointer, and access the value at that location”.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Also note that Indirection operator is one of very few operators which makes pointer expressions as ‘modifiable lvalues’ meaning that such expressions represent locations in memory and can be used on left side of assignment ‘=’ operator.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.