answersLogoWhite

0

Remember that a pointer is just a variable containing the memory address of another variable. A pointer to a pointer is no different, other than that the address contains the address of another pointer. You use the * indirection operator to get the value of the variable being pointed at (the address of the other pointer), and the ** indirection operator to get at the value pointed at by the other pointer.

The following example illustrates how to access the values of pointers to int via an array of pointers to those pointers.

The memory address and the value of every variable is displayed for the benefit of clarity.

#include <iostream>

using namespace std;

int main()

{

// Set up an array of pointers to pointers to int variables.

int X = 1, Y=2; // The actual variables.

int* pX = &X; // Pointers to those variables

int* pY = &Y;

int** pp[2]; // Array of pointers to those pointers.

pp[0] = &pX;

pp[1] = &pY;

// Print the address of all variables and their stored values:

cout << "Var\t&Address\tValue" << endl;

cout << "---\t--------\t-----" << endl;

cout << "X\t0x" << &X << "\t" << X << endl;

cout << "Y\t0x" << &Y << "\t" << Y << endl;

cout << "pX\t0x" << &pX << "\t0x" << pX << endl;

cout << "pY\t0x" << &pY << "\t0x" << pY << endl;

cout << "pp\t0x" << &pp << "\t0x" << pp << endl;

cout << endl;

cout << "Note that both &pp and pp return the same value: the address of the array." << endl;

cout << "pp is simply an alias for the memory allocated to the array itself, it is" << endl;

cout << "not a variable that contains a value. You must access the elements of the" << endl;

cout << "array to get at the actual values stored in the array." << endl;

cout << endl;

// Use the array elements to access the pointers and the values they point to:

cout << "Elem\t&Address\tValue\t\t*Value\t\t**Value" << endl;

cout << "----\t--------\t-----\t\t------\t\t-------" << endl;

cout << "pp[0]\t0x" << &pp[0] << "\t0x" << pp[0] << "\t0x" << *pp[0] << "\t" << **pp[0] << endl;

cout << "pp[1]\t0x" << &pp[1] << "\t0x" << pp[1] << "\t0x" << *pp[1] << "\t" << **pp[1] << endl;

cout << endl;

return( 0 );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


C program pointers to pointers examples?

Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;


What always contains a pointer variable?

pointer variable in c contains the address or the location of the other variable. eg- if a=2 and address of a is 2345. b=&amp;a then b is a pointer which contains 2345 which is the address of a. *b gives value of that is 2.


Need for header node in linked list?

The utility of a header node in a linked list is to simplify the functions of adding and deleting elements in the list. If you have a pointer in memory that points to the first element, then you need to pass the address of that pointer, rather than the value of that pointer, to the routines for list manipulation, because that pointer would need to change if an element were added or deleted at the head of the list. If that pointer, however, points to a special element in the list which is actually the pointer to the head of the list, then you do not need to pass the address of the pointer - you can pass its value, and it will never change. This technique "wastes" the data portion of that first element.


What is dangling pointer reference?

Whenever memory that was in use, and was referred to by a pointer variable, is freed, and the pointer variable is not updated accordingly (setting it to NULL, for example), the pointer variable is considerred to be a dangling pointer reference.

Related Questions

What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


C program pointers to pointers examples?

Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;


How do you display the contents of the memory addresses stored in an element of pointer array?

Every element of a pointer array is a pointer, therefore to access the memory being pointed at by an element, dereference the element. For example: #include&lt;iostream&gt; int main (void) { int* a[10] = {nullptr}; for (int i=0; i&lt;10; ++i) { a[i] = new int (i+1); } std::cout &lt;&lt; "Address of array:\n"; std::cout &lt;&lt; "a = 0x" &lt;&lt; &amp;a &lt;&lt; std::endl; std::cout &lt;&lt; "Address of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = 0x" &lt;&lt; &amp;a[i] &lt;&lt; std::endl; } std::cout &lt;&lt; "Value of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = 0x" &lt;&lt; a[i] &lt;&lt; std::endl; } std::cout &lt;&lt; "Dereferenced value of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; *a[i] &lt;&lt; std::endl; } for (int i=0; i&lt;10; ++i) { delete a[i]; } }


What always contains a pointer variable?

pointer variable in c contains the address or the location of the other variable. eg- if a=2 and address of a is 2345. b=&amp;a then b is a pointer which contains 2345 which is the address of a. *b gives value of that is 2.


Why does array index begins with 0 not with 1?

It is because array name implies its address and if you want to access first element of it pointer address logic is as below: Arrays first element address = array base address + 0 Arrays second element address = array base address + 1


What are pointer to pointer?

A pointer only holds an address information (location) in the memory. if a pointer holds points another pointer then it is a pointer to an other pointer. Pointer holds an address in the memory so in that address there is an other location information that shows another location.


How are the members in the linked list liked together?

Usually each member has a pointer storing the address of the next element.


What is pointer of pointer?

pointer is the variable that holds the address of another variable


Define pointer to pointer in c?

Double (**) is used to denote the double pointer. As we know the pointer stores the address of variable, Double pointer stores the address of any pointer variable. Declaration : int **ptr2Ptr;


Need for header node in linked list?

The utility of a header node in a linked list is to simplify the functions of adding and deleting elements in the list. If you have a pointer in memory that points to the first element, then you need to pass the address of that pointer, rather than the value of that pointer, to the routines for list manipulation, because that pointer would need to change if an element were added or deleted at the head of the list. If that pointer, however, points to a special element in the list which is actually the pointer to the head of the list, then you do not need to pass the address of the pointer - you can pass its value, and it will never change. This technique "wastes" the data portion of that first element.


Difference between void pointer and null pointer?

A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Before ANSI, char pointers are used as generic pointer. Generic pointer can hold the address of any data type. Pointers point to a memory address, and data can be stored at that address.


What is dangling pointer reference?

Whenever memory that was in use, and was referred to by a pointer variable, is freed, and the pointer variable is not updated accordingly (setting it to NULL, for example), the pointer variable is considerred to be a dangling pointer reference.