Pointers to Pointers

Someone explain what is happening below.


• c has type char** and a value of 8092
• *c has type char* and a value of 7230
• **c has type char and a value of 'z'
1
2
3
4
5
6
7
8
| Location | Type    |Value | 
+----------+---------+------+
| unknown  | char ** | 8092 |
+----------+---------+------+
| 8092     | char *  | 7230 | 
+----------+---------+------+
| 7230     | char    | 'z'  |
+----------+---------+------+

1 is a pointer to a pointer to a char.
2 is a pointer to a char.
3 is a simple char.
1.) char** is a pointer to a pointer. The value 8092 is an address to another pointer.
2.) char* is a pointer to a char. The value 7230 is a memory address to a char.
3.) **c is the actual value being pointed to by c.

EDIT:
I like AbstractionAnon's explanation better. :P
Last edited on
I still get no idea what is happening.
Do you understand what a pointer is?
A pointer points to a location in memory and reads its value. Also, known as "Dereference Operator".
Um... that's extremely imprecise, and liable to confuse you.

A pointer is a variable that holds a memory address. The type of the pointer tells the compiler how to treat the data that is held at that address. That is all.

A pointer doesn't "read" anything, and it isn't an operator. It's just a number, and that number is an address in memory.

So, the number you see in c is a memory address. If you dereference it by evaluating *c, you get the data at that address. Since c is a char**, the data at that address is a char *. In other words, the data at that address is another address - another number.

If you dereference c, by evaluating *c, you get that second address. Since *c is a char*, the data at the address is a char. If you dereference *c by evaluating **c, you get that char.

So:

c is a char**, which means it's an address, so it's a number.

*c is a char*, which means it's an address, so it's a number.

**c is a char, which means it's a char.

Last edited on
Note that * has different meanings in different contexts.
char *p //* declares p as a pointer. Is not an operator here.

*p = 'z' //here, * is the dereference operator
Last edited on
Topic archived. No new replies allowed.