The C pointers that point to another pointer

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TP)

Other info: none

To do: Displaying data and memory address using pointer that point to another to pointer

To show: How to use pointers that point to another pointers in C programming

 

 

// Pointer to a pointer

#include <stdio.h>

 

int main(void)

{

int **theptr;

int *anotherptr;

int data = 200;

 

anotherptr = &data;

 

// assign the second pointer address to the first pointer...

theptr = &anotherptr;

 

// printing the related data

printf("The actual data, **theptr = %d\n", **theptr);

printf("\nThe actual data, *anotherptr = %d\n", *anotherptr);

printf("\nThe first pointer pointing to an address, theptr = %p\n", theptr);

printf("\nThis should be the second pointer address, &anotherptr = %p\n", &anotherptr);

printf("\nThe second pointer pointing to address(= hold data),\nanotherptr = %p\n", anotherptr);

printf("\nThen, its own address, &anotherptr = %p\n", &anotherptr);

printf("\nThe address of the actual data, &data = %p\n", &data);

printf("\nNormal variable, the data = %d\n", data);

 

return 0;

}

 

Output example:

 

The actual data, **theptr = 200

The actual data, *anotherptr = 200

The first pointer pointing to an address, theptr = 0012FF54

This should be the second pointer address, &anotherptr = 0012FF54

The second pointer pointing to address(= hold data),

anotherptr = 0012FF48

Then, its own address, &anotherptr = 0012FF54

The address of the actual data, &data = 0012FF48

Normal variable, the data = 200

Press any key to continue . . ..

 

 

C and C++ Programming Resources | C & C++ Code Example Index