The C Programming Language - Pointers
This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher
This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
As you can see, working with pointers can be a bit confusing. <strong>The</strong>refore, here is a brief<br />
summary of how pointers are used:<br />
char ch = 'A';<br />
int a = 0, y = 10;<br />
float z = 0.0, x = 3.14;<br />
int *iptr1 = (int *)0x0061ff14; // typecast to an int* - iptr1 points to 0x0061ff14<br />
int *iptr_a;<br />
// undefined int pointer<br />
int *iptr_y = NULL;<br />
// int pointer initialized to NULL<br />
float *fptr_x;<br />
// undefined float pointer<br />
fptr_x = &x;<br />
char cptr_ch = &ch;<br />
// defined float pointer - fptr_x points to x<br />
// defined float pointer - fptr_x points to x<br />
iptr_y = &y;<br />
// iptr_y stores the address of y<br />
*iptr_y = 1000; // iptr_y points to y -> now y = 1000;<br />
a = *iptr_y; // a stores the value pointed to by iptr_y -> a = 1000<br />
*iptr_y = a + 500; // y = 1000 + 500 -> now y = 1500<br />
iptr_a = &a;<br />
// iptr_a stores the address of a<br />
printf("%d\n" , *iptr_a);<br />
printf("%p\n" , iptr_a);<br />
// displays the value a<br />
// displays the Address of a<br />
1.4 Pointer of <strong>Pointers</strong><br />
A pointer to a pointer contains the address of a pointer to which it points. A simple<br />
pointer contains the address of a variable. However, when a pointer points to another<br />
pointer, the first pointer contains the address of the second pointer, which in turn<br />
points to the variable that contains a value.<br />
In the following figure, the pointer pptr points to another pointer ptr, which in turn<br />
points to the variable b. With double dereferencing, you can use the pointer ptr to<br />
access the variable b directly.<br />
Figure 1: Pointer to pointer<br />
<strong>The</strong> C <strong>Programming</strong> <strong>Language</strong> by Heimo Gaicher – Chapter <strong>Pointers</strong> 14