Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Behar
487 PointsQuestion about using a pointer as a parameter when declaring makeSphere()
When defining the makeSphere() function, a pointer is used as a parameter for the array. I am trying to understand why you don't have to add a & in front of the passed in array variable when calling makeSphere. Doesn't the array pointer variable need an address assigned to it?
1 Answer
Robert Russell
8,958 Points& is used to change the pointers address as the reference operator. Once you set int * ptrWhatever = &circleThings, the variable ptrWhatever has the address of circleThings stored in it. If you use &ptrWhatever, you'll be passing the address of where the address of circleThings is stored (read carefully, no typos here). Read the indirection operator (*) as "the contents at", and the reference operator (&) as "the address of".
Something like this: int circleThing = 563; //circleThing is 563. int *ptrWhatever = &circleThing; //ptrWhatever is now 0xA84456 (or something like that) int *ptrToPtr = &ptrWhatever; //ptrToPtr will be different, like 0xA8445A (i think ints are typically 4 bytes)
//so if you output *ptrWhatever, like std::out << *ptrWhatever; //you'd get 563. but if you output ptrWhatever, you'll get 0xA84456; //and even further if you output *ptrToPtr, you'd get the contents at the address it's looking, so it looks at 0xA8445A, because that's the value held by ptrToPtr, and you'd get 0xA84456, because that's the contents at the address stored in ptrToPtr. I don't know if this just confused you more or not, but pointers are a difficult subject to get a grasp on. Try reading a few different tutorials that explain them on the web until you find one that explains it in a way that's easy for you to understand. Don't be afraid of looking at C and C++ examples either, they won't be too far off what you're looking for.
Daniel Behar
487 PointsDaniel Behar
487 PointsThank you for that in-depth response!