Archived

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

Leadorn

What does this do int **baken

Recommended Posts

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
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