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 trial

iOS Objective-C Basics (Retired) Pointers and Memory Structs

Kingsley Peh
Kingsley Peh
450 Points

Is it mandatory to use a * annotation to accept array as parameter in a function ?

Any special reasons for doing so, and can we do it for accepting other data types(int, float etc.) in the function ?

I was referring to the snippet of codes(para-coded it) below in the tutorial:

Sphere makeSphere(float *c, float r); int main() { float c[] = {1.11, 2.22, 3.33}; float r = 4.44; }

Sphere makeSphere(float *c, float r) { … }

1 Answer

Stone Preston
Stone Preston
42,016 Points

In c, arrays are automatically transformed into pointer notation when passed as an argument. so

Sphere makeSphere(float *c, float r) { … }

and

Sphere makeSphere(float c[], float r) { … }

will be interpreted the same.

Yes you can use pointers to any type as function arguments, the difference between using pointers and just regular types is that regular types are passed by value, which means that the function acts on a copy of that variable, it doesnt change its value at all, it just modifies a copy thats used inside the function. Pointers are passed by reference, which means a function DOES modify that variable.