Archived

This topic is now archived and is closed to further replies.

-dcx-

Pointer to Pointer...

Recommended Posts

Can someone explain to me the concept of a pointer pointing to another pointer? I don''t quite understand how it works. Some sample code would be useful if anyone is willing to help.

Share this post


Link to post
Share on other sites
the most useful code example i can think of is when you're trying
to create dynamic multi-dimensional arrays:


int **2DArray; // (notice that its not created yet, just declared as a pointer. no memory is allocated at this point)

// then somewhere else in your code

2DArray = (int**) new int[SizeX];
for(int j = 0; j <= SizeX; j++)
2DArray[j] = (int*) new int[SizeY];

// then reverse for cleanup
for(int j = 0; j <= SizeX; j++)
delete [] 2DArray[j]
delete [] 2DArray

now you can access 2DArray like so:

2DArray[x][y] = myint;

for a 3rd deminsion you'd creat a pointer to a pointer to a pointer
and so on...
hope this was of some help..

[edit: damn italic tags.. heh]

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

'In C we had to code our own bugs. In C++ we can inherit them.'

[edited by - eldee on October 17, 2002 4:53:51 PM]

[edited by - eldee on October 17, 2002 4:56:14 PM]

Share this post


Link to post
Share on other sites
a pointer simply holds the address of a space in memory. you make pointers of a certain type, say int *, so that the compiler knows to expect a certain type of variable at that address in memory. pointers themselves are stored in memory and thus also have an address in memory. a pointer to a pointer simply holds the address of the object pointer in memory.

a pointer to a pointer is often referred to as a handle. handles are useful for doing stuff that changes the value of a pointer to an object (i.e. point the pointer to a new object). to do that in a function you need to be able to effect the value of the pointer. for instance, lets make a dumb function to change the value of a pointer:


void changeThis(objectType ** obj) {
if (someCondition) {
delete *obj;
*obj = new objectType;
}

return;
}


sorry, i can''t think of a more particular use for handles b/c i don''t often use them. hopefully someone else can come up with a more concrete example.

-me

Share this post


Link to post
Share on other sites
Thanks for the explanations but things are still sketchy for me. Here''s a sample code from the lecture in my class today. Maybe someone can explain it to me.

(I might have copied some part wrong, so if you think you see an error, feel free to point it out)

 
int **ipp;
int i = B;
int *ip = &i;
ipp = &ip
int k = **ipp;
int *ip2 = *ipp;


I''m not sure about the i = B part (I was sitting kind of far), it might have been 8 or 118. Also when using this example, I know he used the numbers 4 and 118 as examples, I guess assuming those numbers were already stored in memory (so i = B might actually be i = 118, but I''m not sure).

Oh and before anyone asks, I did try to go to the office hours but there was a somewhat long line and with my schedule, there wasn''t enough time to wait.

And if you think this example is to vague or isn''t clear enough, feel free to provide another example of your own.

Share this post


Link to post
Share on other sites
 
int **ipp;
int i = B;
int *ip = &i;
ipp = &ip
int k = **ipp;
int *ip2 = *ipp;


This example really sucks, since you would never see anything like it in real life, but I guess that''s how academics go; gotta pack everything into as small a space as possible, even if it becomes less cogent and coherant...

Anyway, here''s a breakdown.

ipp is declared to be a pointer to int pointer. This means that its value will be the memory address of an int pointer, whose value will in turn be the address of an int, whose value will in turn be a number (I think your ''B'' is supposed to be a number; we''ll use 118). Conveniently, we have an int to point to called i. In the third line, we create our int pointer, ip, and assign the memory address of i to it, using the address-of (&) operator. k exists to demonstrate what''s called dereferencing; by using the value-at (*) operator, we dereference ipp to an int pointer and then dereference that value to an int, which is literally the value of i, and is now also the value of k. ip2 is another int pointer used to show that *ipp really is an int pointer, and can be used as such.

Ask if you need further explanation.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Okay I think I'm starting to get it. Can you just expand on your previous explanation a little bit more.



[edited by - -dcx- on October 17, 2002 12:16:05 AM]

Share this post


Link to post
Share on other sites