• Advertisement

Archived

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

What does this do int **baken

This topic is 5502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I’ve never understood this double **. Is it a pointer to a second pointer that points forward to a third variable. [edited by - leadorn on January 19, 2003 1:51:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
So it automatically goes forward when reaching a double pointer. Can I user triple....?

Cant get it to work, can you give me an example please?

Share this post


Link to post
Share on other sites
You can use them to dynamically allocate 2d arrays


int **myl33tArray;

myl33tArray = new int*[ARRAY_WIDTH];

for (int i = 0; i < ARRAY_WIDTH; i++)
myl33tArray[ i ] = new int[ARRAY_HEIGHT];


Access it like: myl33tArray[2][4] = 100;

[edited by - glassJAw on January 19, 2003 2:26:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
that remeinds me of 2d arrays...

Share this post


Link to post
Share on other sites
I have one more q!

if i use your 2d array, is this the correct way to deallocate the array?


  
alloc
int **Map;
Map = new int*[ARRAY_WIDTH];

for (int i = 0; i < ARRAY_WIDTH; i++)
Map[ i ] = new int[ARRAY_HEIGHT];


dealloc
for (unsigned int i = 0; i < ARRAY_WIDTH; i++)
{
delete [] Map[i];
}

delete [] Map;

Share this post


Link to post
Share on other sites
One more use for pointers to pointers is in linked lists.

Normally you would have the following to add a member to a link-list...

  if (pFirst == NULL)
pFirst = pNew;
else
{
STRUCT *p;

p = pFirst;
while (p->pNext != NULL)
p = p->pNext;

p->pNext = pNew;
}
pNew->pNext = NULL;


Or you could have the following, which removes the special case for the head of list...

  STRUCT **ppNext;

ppNext = &pFirst;
while (*ppNext != NULL)
ppNext = &((*ppNext)->pNext);

*pNext = pNew;
pNew->pNext = NULL;




PS: Yes you can have triple pointers, or for that matter quad-pointers, or an depth of pointers to pointers.

[edited by - Beelzebub on January 31, 2003 5:44:32 PM]

Share this post


Link to post
Share on other sites

  • Advertisement