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:
/* * 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”.
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.
- Check C Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs