Pointer indirection
-
Hi, Is this how it should be done? int Strawberry = 10; int * PointerInd0 = &Strawberry; int ** PointerInd1 = &PointerInd0; // I’m not sure what comes next int *** PointerInd2 = &PointerInd1; Sorry I’m not enclosing the source code in code tags, I don’t have edit options on mobile.
Calin Negru wrote:
// I’m not sure what comes next
int *** PointerLvl3 = &PointerLvl2;Yes, indeed. You can continue with as many levels of indirection as you wish (although I don't remember needing more than two).
Mircea
-
Calin Negru wrote:
// I’m not sure what comes next
int *** PointerLvl3 = &PointerLvl2;Yes, indeed. You can continue with as many levels of indirection as you wish (although I don't remember needing more than two).
Mircea
If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess
-
If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess
Calin Negru wrote:
***PointerInd2 = 31; // just a guess
Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:
**PointerInd2 = 31;
compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.
Mircea
-
If that is true then this simple assign address to is a bit deceiving because you still have to remember what PointerInd2 is, you need special syntax to access Strawberry from PointerInd2 ***PointerInd2 = 31; // just a guess
Correct, I think. A simple test program would confirm that, but I'm not feeling the urge. But if you need more than two or three levels of indirection, then the problem space should lead to sensible variable naming. That and intelligent comments should make your intent clear. And, of course, you've got
typedefs
or C++using
statements to help reduce the brain cramp that I find multiple indirection sometimes brings. If you're using C++, you also havereferences
which might help reduce the (apparent) levels of indirection going on.Keep Calm and Carry On
-
Hi, Is this how it should be done? int Strawberry = 10; int * PointerInd0 = &Strawberry; int ** PointerInd1 = &PointerInd0; // I’m not sure what comes next int *** PointerInd2 = &PointerInd1; Sorry I’m not enclosing the source code in code tags, I don’t have edit options on mobile.
int Strawberry = 10; int \* PointerInd0 = &Strawberry; std::cout << "\*PointerInd0: " << \*PointerInd0 << std::endl; int \*\* PointerInd1 = &PointerInd0; std::cout << "\*\*PointerInd1: " << \*\*PointerInd1 << std::endl; // I’m not sure what comes next int \*\*\* PointerInd2 = &PointerInd1; std::cout << "\*\*\*PointerInd2: " << \*\*\*PointerInd2 << std::endl; int \*\*\*\* PointerInd3 = &PointerInd2; std::cout << "\*\*\*\*PointerInd3: " << \*\*\*\*PointerInd3 << std::endl;
Results:
*PointerInd0: 10
**PointerInd1: 10
***PointerInd2: 10
****PointerInd3: 10And so until the compiler or the application gives up. If you are really interested then get an assembly listing and see what the machine code is doing.
-
Calin Negru wrote:
***PointerInd2 = 31; // just a guess
Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:
**PointerInd2 = 31;
compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.
Mircea
>I don’t see why you see it as deceiving It’s just a single &sign someone could expect it to behave like a standard pointer , instead you have to track down the indirection pointer to where it was declared and observe all the detail (the extra stars)
-
int Strawberry = 10; int \* PointerInd0 = &Strawberry; std::cout << "\*PointerInd0: " << \*PointerInd0 << std::endl; int \*\* PointerInd1 = &PointerInd0; std::cout << "\*\*PointerInd1: " << \*\*PointerInd1 << std::endl; // I’m not sure what comes next int \*\*\* PointerInd2 = &PointerInd1; std::cout << "\*\*\*PointerInd2: " << \*\*\*PointerInd2 << std::endl; int \*\*\*\* PointerInd3 = &PointerInd2; std::cout << "\*\*\*\*PointerInd3: " << \*\*\*\*PointerInd3 << std::endl;
Results:
*PointerInd0: 10
**PointerInd1: 10
***PointerInd2: 10
****PointerInd3: 10And so until the compiler or the application gives up. If you are really interested then get an assembly listing and see what the machine code is doing.
Thank you Richard, you make it clear.
-
Calin Negru wrote:
***PointerInd2 = 31; // just a guess
Yes, nothing wrong with that. I don't see why you see it as deceiving. If you miss one of the indirection operators and you write:
**PointerInd2 = 31;
compiler will flag it with an error like "cannot assign 'int' to 'int*' ". But, again, cases where you need more than two level on indirection are exceedingly rare. Probably because we humans aren't great at keeping track of stack levels.
Mircea
>I don’t see why you see it as deceiving It’s just a single &sign one that could make you believe it’s a typical pointer you’re dealing with. You need to be in a high alert so to speak and always have in mind what you have declared.
-
>I don’t see why you see it as deceiving It’s just a single &sign one that could make you believe it’s a typical pointer you’re dealing with. You need to be in a high alert so to speak and always have in mind what you have declared.
-
Calin Negru wrote:
you believe it’s a typical pointer you’re dealing with.
The developer should of course be aware of the code they are writing/modifying. And this should not be something that is used very often.
>The developer should of course be aware I know. If you look at the address assignation alone you don’t have a clue what that is (Something = &SomethingElse) you have look at the declaration to figure it out. Sorry if that sounds like repeating the same thing. What I’m saying is that it’s not your C# fire and forget.
-
Thank you Richard, you make it clear.