Advertisement

passing a pointer to a pointer

Started by January 10, 2003 05:18 PM
1 comment, last by Viajero 21 years, 10 months ago
If you make a pointer like this, ... CLASS CBLAH {/*blah*/}; ... CBLAH *blah; blah=new CBLAH[10]; and a function to resieve it''s address like this : void func(CBLAH **blah) ... why is the function called like this : func(&blah); and not like this? : func(blah); ( or func(&blah[0]); ) Would the case be the same if blah was "an actual array" (CBLAH blah[10]? In my experiense those two are basicly the same thing.. &blah to me seems as if trying to get the address of the address of blah ( &&blah[0] ) Pardon my ignorance..
Arrays and pointers are treated very similarly in C++ if I''m not mistaken, but here''s how it''s working out in your code:

blah ------> CBLAH0, CBLAH1, CBLAH2,...,CBLAH9

Here blah is pointing to a memory location (the address of the first CBLAH element with the rest following it). blah itself has a memory location too (&blah).

So the function that takes a double pointer wants the address of the pointer that points to the array, or &blah. IIRC, an array is essentially a pointer that points to the first element of the array, so it would be identical syntax if blah was a static array.
-YoshiXGXCX ''99
Advertisement
Ah, of course, &blah is the address of blah or in other words the address of the address of CBLAH[0] (kinda). So a faster and cleaner way would be to define the pointer in the function: CBLAH *blah and give it the address of CBLAH[0] (blah or &blah[0]) so that the information would transfer directly between the function''s pointer and CBLAH[0] instead of trough the blah array (or should I say pointer). Ahh, I love pointers..

This topic is closed to new replies.

Advertisement