pointer to pointer

The following is the memory layout of a pointer to a pointer.
Let's assume an 8 bit computer with 8 bit addresses (and thus only 256 bytes of memory). This is part of that memory (the numbers at the top are the addresses):

45 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | 58 | | | 63 | | 55 | | | h | e | l | l | o | \0 | |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
What you can see here, is that at address 63 the string "hello" starts. So in this case, if this is the only occurrence of "hello" in memory then,

const char *c = "hello";
... defines c to be a pointer to the (read-only) string "hello", and thus contains the value 63. c must itself be stored somewhere: in the example above at location 58. Of course we can not only point to characters, but also to other pointers. E.g.:

const char **cp = &c;
Now cp points to c, that is, it contains the address of c (which is 58).
________________________________________________________________________

What I would like is a brief memory layout of a pointer to a pointer of a node or struct of a linked list. What is the value of **head, *head, head in the insertFront routine. Hope Im not asking to much.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct node{
		int  data;
		node *next;
	};


void initNode(struct node *head,int nodeData)
{
		head->data = nodeData;
		head->next = NULL;
}

void insertFront(struct node **head,int frontData)
	{
		node *frontNode = new node;
		frontNode->next = *head;
		frontNode->data = frontData;
		*head = frontNode;

int main()
{
  node *head = new node;
  initNode(head,10);
  insertFront(&head,60);  

return 0 ;    
}
Last edited on
pointers are variables that store the value of a memory address of another variable of the same type

pointer to pointer stores the memory address of a pointer

I think the word pointer makes the idea of pointers hard to understand. It almost makes pointers seem like some mystical property of the c/c++ language that just points and does not have an actual value. When in fact they do have a value (the memory address they hold) and they have a memory address where they are stored just like any other type.

1
2
3
4
5
6
7
8
9
10
11
12
13
void insertFront(struct node **head,int frontData)
{
        // the value of head is the value of the memory address of *head hence head
                    // stores the address of another pointer
        // the value of *head is the value of the memory address of **head hence 
                    // *head stores the memory address of the underlying node
        // **head is an object of type node

	node *frontNode = new node;
	frontNode->next = *head;
	frontNode->data = frontData;
	*head = frontNode;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

struct ptr
{
	int a;
	int b;
	friend bool operator == (const ptr& lhs, const ptr &rhs);
};

bool operator == (const ptr& lhs, const ptr &rhs)
{
	cout << "object compare => ";
	return lhs.a == rhs.a &&
		   lhs.b == rhs.b;
}


int main() 
{
	ptr p = {4, 5};
	ptr *pp = &p;
	ptr **ppp = &pp;
	
	cout << "*pp == p " << boolalpha << (*pp == p) << endl;
	cout << "pp == &p " << boolalpha << (pp == &p) << endl;
	cout << "*ppp == pp " << boolalpha << (pp == *ppp) << endl;
	cout << "**ppp == p " << boolalpha << (p == **ppp) << endl;
	cout << "ppp == &pp " << boolalpha << (ppp == &pp) << endl;
	
	cout << (*pp).a << " " <<
			pp->a << " " <<
			(**ppp).a << " " <<
			p.a << endl;
			
	return 0;
}
Last edited on
Topic archived. No new replies allowed.