I can't seem to find practical use of pointers in objective language. I know c# and java and there is no need to use pointers (thats what i think and it is also considered as dangoures coding by c# and java) but now im starting to learning objective-c and i cant figure out why you have to always create pointer to an object instance.
-
Oh, theres tons of *practical* uses for pointers. Now if you want to talk about the pros and cons of pointers vs references for memory management, then you might want to rephrase your question. – Perception May 14 '12 at 11:21
-
It might be because Objective-C is just a layer on top of C, which uses pointers extensively. Also, Java objects are in essence a pointer, just without the *. – Paaske May 14 '12 at 11:23
-
possible duplicate of [Objective-C and Pointers](http://stackoverflow.com/questions/5203284/objective-c-and-pointers) – jscs May 14 '12 at 18:11
4 Answers
In Objective-C (as in C) arguments to a function are passed by value, If you want your function to modify the original object, you have to pass a pointer to that object. Java and c# pass arguments by reference, so have no need for pointers.

- 170
- 2
- 8
well, in c# you do actually use pointers, tho not explicitly. Think of delegates, anonymous types etc etc. the garbage collection in c# can also take a hit if you are not managing your use of these reference types efficiently.
in obj-c, the playing surface is a little more exposed. I'd imagine that approach has as many cons as it has pros for exactly the reasons you stated above re the danger of memory gpf's etc.
slowly slowly, catchie monkey :)

- 22,305
- 4
- 49
- 63
Do you actually understand what pointers are ? That it is just an address to the memory space where the object is stored.
So if you work with pointers, and you pass the pointer to a function as a parameter, you only pass the address and not the complete object. This is much faster, because you don't have to copy the complete object, you just can access the values of the object through the address.
Java also uses pointers, you just don't have to worry about them yourselves. (Which in some cases is pretty annoying if u don't want to use pointers.)
Edit
int nvar=2;
int* pvar=&nvar;
here you can see that if I take a reference of the integer "nvar", I can put it in the pointer to integer "pvar". So that's the connection between pointers and references ( in C++, in this case )

- 572
- 3
- 15
-
I know pointers hold address to object... but why couldnt you just use reference as in c# or java? – gorgi93 May 14 '12 at 11:28
-
As mentioned by other people, pointers are used for tons of things. Polymorfisme for instance, is only possible with pointers. If you take a reference of an object (this can be an integer too), you receive the address of the object, and you are actually working with pointers then. I'll add something extra in my answer above. – Anonymous May 14 '12 at 11:37
One of the things that you can do with pointers and cannot do with references is incrementing them. Since pointer is an address (what has already been mentioned), changing it's value makes it point to the next/previous object in an array. It is not very useful since you can just use next/previous index on the array, but in some situations it may produce a teeny-tiny faster code (although dirtier and more error-prone).
Pointers are quite fun, when you get used to them ;)

- 150
- 1
- 8