Pointers pointers pointers....

• 11-01-2001
G'n'R
Pointers pointers pointers....
Hi,

I am mucking around with pointers in a project and got a questions. The code below is not complete but it illustrates what i dont get.

main(){
/* defined elsewhere */
struct aStruct *f=NULL, **ff = NULL, ***fff = NULL;

f = getStruct(); /* gets a struct using malloc */

/* I dont get why i have to asign the address of the pointer
since f already is a pointer and i though a pointer is a
address and ff=f would do */

ff = &f;
(*fff)->x=54321;

etc...

/* Same here. I dont get why i have to asigne the address
of the pointer since i though a pointer is a address and
fff=ff would do */

fff = &ff
(**fff)->x=12345;
etc....

free(f);

}

G'n'R
• 11-01-2001
Salem
• 11-01-2001
Garfield
Well, what you don't understand (I think) is the levels of indirection. When you have:

ff = &f;

You have f (which is declared as *f) and ff (which is declared as **ff). Notice the 2 asterisks for ff. This tells you that you the level of indirection of 2 for ff. Now, if you want to assign, you have to match the levels of indirection. Since f only has one, you have to make a pointer out of a pointer (which is 2 levels of indirection). Then you can assign the value f to ff. Understand?

--Garfield
• 11-01-2001
jasrajva
to put it simply

imagine

struct aStruct e;
struct aStruct *f = NULL, **ff = NULL....etc

now to assignthe address of the structure e to f
you can do
f = &e;
bcoz f is a pointer to the structure

now ff is a pointer to the pointer of type aStruct so

ff must contain a pointer to f not to e

as would happen if you say ff = f(youmay pop up an error too(or a warning atleast)

so you say ff = &f and assign the address where f is stored to ff

and so on for fff
• 11-01-2001
Sayeh
Many times it is advantageous (necessary even) to work with the address of a pointer that points to another pointer or data block.

Levels of indirection. It is called indirection because you are not working with the 'contents', but rather with an address of a memory location whose contents are the address of another memory location where either a pointer (another address) or a datablock (contents) reside.

This can be useful, for example, in a memory manager where the O/S wants to be able to move blocks around (for compaction and general management/organization of RAM) without invalidating the pointer it has returned to the requester everytime it moves a block.

To do this, instead of handing the requester a pointer to the RAMblock (an address to the block), it hands the requester a pointer to a pointer to a RAMblock. That is a double dereference (or double indirection).

This way, the pointer that gets handed to the requester doesn't change, but the pointer that the memory manager is aware of does, because the block gets moved.

If any of you know a developer at Microsoft, in the O/S group, please have them leave me a note and I will teach them this simple concept.
• 11-01-2001
Garfield
> If any of you know a developer at Microsoft, in the O/S group, please have them leave me a note and I will teach them this simple concept.

Ohhh, nice! Low...but nice.

--Garfield
• 11-01-2001
G'n'R
Okey cool. Thanks. I think i am getting the picuture. But to clearify.

struct aStruct *f=NULL, **ff=NULL, ***fff=NULL;

ff = &f;

Here i am asigning the address of the pointer f to ff. However ff still has its own address in memory, right? So i am pointing to a address which f holds, but at the same time ff has its own address.

fff = &ff
Here i am doing the same thing but with another level of indirection. e.g. I am asigning the address of ff which holds the address of f but fff still has its own address. So i actuall have two addresses "stored" in fff as well as fff having its own.

hmm, that was probable a bad explanation of things , but am i getting close ??

The rest of it i understand.

G'n'R
• 11-01-2001
Salem
> hmm, that was probable a bad explanation of things , but am i getting close ??
Looks good to me
• 11-01-2001
The V.
Everything has an address and contents.

So, think like this:

Code:

``` Name | Address | Contents --------------------------  f    |  &f    |  &struct  ff  |  &ff  |  &f  fff  |  &fff  |  &ff```
Here, f is a pointer (to a struct) whose contents is the address of a structure in memory. ff is a pointer (to a pointer to a struct) whose contents is the address of f. fff is a pointer (to a pointer to a pointer to a struct) whose contents is the address of ff.

Basically:

fff lets us (through indirection) access ff.
ff lets us (through indirection) access f.
f lets us (through indirection) access the struct.

But yes, any piece of memory has 2 parameters: the address and the contents. Think of memory like a big string of numbered boxes -- the address tells us which box number to look at, the contents are what we put in the box. Sometimes, we use a prointer, which is basically a note telling us to look in a different box.

A triple indirection is a lot like the game we've all played as a child, wher you have to go through the house (or city) finding notes, each which tells you where the next note is. In this case, you look on box &fff, and it says "look in box &ff". Box &ff says "look in box &f", and box &f says "look at box &struct". Inside box &struct is the struct you created.
• 11-01-2001
G'n'R
okey, thanks again. I got it. The whole confusen was because the syntax thing , e.g using the & (ampersand) to asign the address of the pointer. I though you didnt have to do that because a pointer is nothing but a address anyway. But then i would be asigning the address directly to the pointer right and not the address of the pointer. hmm, if i got that right , i got it.

But i fully understand the logic here.

Cheers!
G'n'R
• 11-01-2001
The V.
That's right.

For example, f contains the address of the struct. ff is declared as containing "an address to a pointer containing the address of a struct".

If you did force ff = f (which can be forced by casting, although it's completely wrong to do) ff would have the address of the struct, not the address of something with the address of a struct.
• 11-02-2001
G'n'R
okey, got it. Cheers

G'n'R