Maybe Not quite Drowning (Yet!)


Struggling here! It seems that one reason we’re fiddling with pointers and pass-by-reference is to get around the fact that functions in C can only return a single value, is that correct? if we could define cartesianToPolar to return 2 floats (which we can’t) then PBR here wouldn’t be necessary?

Also, really, cartesian to polar co-ordinates? I would guess many of us are struggling with pointers etc and this cannot be the best type of example to use, adding trigonometry into the mix as well. I know in theory we could ‘abstract that away’ and just focus on the PBR etc but honestly it does make this even harder :frowning:

Finally :slight_smile: What IS M_PI_2? It just appears in the code for cartesianToPolar but I can’t see it defined or explained anywhere?



Joy of random clicking! I have found M_PI_2 :slight_smile:


A function can return any number of values provided you wrap them in a struct, but this incurs a cost.
For example:

struct Polar
   float angle;
   float distance;

struct Polar cartesianToPolar (float x, float y);
struct Polar cartesianToPolar (float x, float y)
     struct Polar polar;
     polar.angle = ...;
     polar.distance = ...;
     return polar;
struct Polar polar = cartesianToPolar (M_PI_2, M_PI_2);

As for fiddling with pointers and PBR, you had better learn all about them if you want to be a good programmer.


Well I’ll keep working through the book, but it IS my source of information, so I’m hoping there will more helpful explanations about pointers etc later on when we actually start to use them. I understand that the writing of the book must have been a huge undertaking, especially as I would guess that many of us working through it are at very different levels of comprehension and understanding, but it does feel sometimes as if t’s just a bit too far out of reach :frowning:


The Objective-C Programming Language is a super set of the C Programming Language. Thus it is worth investing some time in reading the following book.

The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie.

Once you get a good background in C, you will find the Objective-C a pleasant language to learn.


Thanks, I had hoped I wouldn’t have to wander around several books until I had finished this and so had a sound foundation to build on, but maybe that’s not possible.