Archived

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

dynamicman

Is this double pointer correct?

Recommended Posts

I just need to understand a bit more about double pointers... I want a pointer that points to another pointer thats an array of pointers to stuff...
    
CStuff** ppstuff;
CStuff* pstuff = new CStuff[15];
  
Then I'm not sure if I should do this...
  
ppstuff = &pstuff;
  
or...
  
(*ppstuff) = pstuff;
  
To access the array I use this...
  
ppstuff[4]->method();
  
is that correct? whould this last code access the 5th pointer in my array? [/source] Edited by - dynamicman on October 30, 2001 7:01:18 PM

Share this post


Link to post
Share on other sites
Well, your second assignment, (*ppstuff) = pstuff, is a recipe for disaster, since you''re basically trying to assign the value of pstuff to the memory pointed to by ppstuff--which is uninitialized.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"(*ppstuff) = pstuff, is a recipe for disaster."

Are u sure?

*ppstuff is an address(a pointer to CStuff), and dman here is assigning it with pstuff(a pointer to CStuff), also an address ''which'' he has already initilised via the statement "CStuff* pstuff = new CStuff[15];".

Dynamicman is not assigning values, but referencing addresses here.



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ppstuff is null so *ppstuff is invalid. The first form is correct. The call ppstuff[4]->method() is also correct.

Share this post


Link to post
Share on other sites