-1

I am a beginner in Objective-C and I am having trouble grasping the difference between pointers and variables. For example, what is difference between

int number = 30;

int *pointerNumber = 30;

I would be really grateful if someone could explain this to me.

  • 2
    This is a very broad question that has many good answers on the internet and in books. Can you be a bit more specific? What do you understand, what don't you understand? – Sebastian Dec 17 '13 at 03:53
  • Sometimes it's easier to learn just by asking a person who you can directly question than consult a text. – Zarathuztra Dec 17 '13 at 04:11
  • A pointer is just an arbitrary location in the machine's RAM. Usually it "points" to some data stored in RAM but ultimately all it is is a number, 32bit integer on 32bit processors and 64 bit integer on 64 bit processors. Objective-C doesn't have pointers, but it inherits them from C and uses them all the time. – Abhi Beckert Dec 17 '13 at 04:11
  • 1
    possible duplicate of [What is a pointer?](http://stackoverflow.com/q/153874) See also [C pointers good tutorials](http://stackoverflow.com/q/2271490) and maybe [What are the barriers to understanding pointers and what can be done to overcome?](http://stackoverflow.com/q/5727) and [What do people find difficult about C pointers?](http://stackoverflow.com/q/4025768) – jscs Dec 17 '13 at 04:11
  • I think those are good questions to consult, but putting it in an Objective-C context I think would benefit the OP better. @AbhiBeckert I have to disagree with you regarding Obj-C and pointers. Obj-C is a superset of C and therefore has pointers by default. Without C there is no Obj-C – Zarathuztra Dec 17 '13 at 04:12
  • 1
    @Zarazthuztra yes, as I said they're used all the time. But they exist in C, and so you should look up the C language to find out how they work. Putting it in "objective-c context" doesn't achieve anything, if you don't learn C you cannot learn Obj-C properly, and since Obj-C is a superset of C you should learn C first. – Abhi Beckert Dec 17 '13 at 04:16
  • @AbhiBeckert I agree with you on learning C first, as that's a great place to start. However, you could just as easily learn what a pointer is just doing Obj-C as well. There's no difference in how they work between the two languages. – Zarathuztra Dec 17 '13 at 04:22

2 Answers2

1

A pointer is simply a handle, location, or memory address. So you with a pointer, you're not directly manipulating a thing, you're manipulating something in memory at which the pointer points.

However, with a scalar value like an int, you are working directly with that piece of data. How a given language uses pointers and values is up to the language, so there are implications as to where they are stored, how they are accessed, etc.

That's not the whole story, but it's the easiest way to explain it IMHO.

Now, in the context of Objective-C, you wouldn't really find many pointers to an int type, you would find an int value. Objects on the other hand use pointers like crazy, and it's what allows you to pass around an Object to a method, or send it in a message, modify it, and still have the same object, just mutated.

However, with that int, passing it as a message parameter will surely pass the value of the int, but not a reference to it, so the original int will remain intact.

Here is some documentation on pointers

And here is an example in Objective-C to point it out

Let's assume for a moment we have a simple iOS or OSX app following Apple's Objective-C standards and libraries. Let's also assume we have a class, Foo, with the appropriately defined header

#import Foo.h
#import Bar.h
@implementation Foo
    - (void) doSomethingWithPointer:(Bar)obj{
        obj.x = 5;
    }

    - (void) doSomethingWithValue:(int)num{
        int x = num;
    }

    - (void) doThings:{
    //Suppose I have an object of type Bar available to me. I'm going to get a pointer to a new instance of that object, freshly allocated into memory
    Bar *barObj = [Bar alloc];
    //we will assume Bar has only one feature, a publicly accessible property called x. It's of the int type, of value 1, but again, is part of the Bar object that we are POINTING to.

    //Now, here is a variable that is NOT a POINTER, it's merely a value in memory.
    int num = 1;

    //Let's call the two methods in this class that perform some action and see what happens.

    //first, our pointer
    NSLog(@"%d",barObj.x);//outputs 1
    [self doSomethingWithPointer:barObj];
    NSLog(@"%d",barObj.x);//outputs 5

    //Now, let's call on our variable that is just a  value and NOT a POINTER
    NSLog(@"%d",num);//outputs 1
    [self doSomethingWithValue:num];
    NSLog(@"%d",num);//outputs 1

}
@end

What I have demonstrated here is not just the concept of a pointer, but also of passing by reference. Aside from that, I have also shown what the difference is between a pointer and a scalar value represented by a variable. When we passed "variable", as you call it, to a method, it's modified in the method, but as you can see the original was kept in tact and did not change after what was used in the method was. Only the value was passed and not the reference to that original value.

However, in the case of the object, we passed a pointer to the object. Since we modified the object represented by that point, at THAT location in memory, the value inside the object DID change because we were in essence acting on the same object. We knew where to look for it in memory.

If this is still not making sense just let me know in the comments and we'll figure this thing out :)

I'm going to assume you come from either a scripting or Java background if you don't know pointers that well ;)

As a side note, why is this the case only with objects in obj-c it would seem? Objects, or rather instances of Classes, encapsulate data, and that data needs to be able to remain intact and passed around, but if mutated, that mutation needs to also be reflected in the object's state no matter where that same instance of the object is used.

Community
  • 1
  • 1
Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • Thank you so much for this detailed response! I now clearly know the difference between a pointer and a primitive value. Also, I'm coming from Visual Basic. – forrestgumpcookies Dec 20 '13 at 03:27
  • I'm so glad it helped! And I see, another non-pointerific language ;) Technically I should clarify, I know in Java we called them primitives, the generic term is scalar value: http://en.wikipedia.org/wiki/Variable_%28computing%29 – Zarathuztra Dec 20 '13 at 03:29
-1

variable contains the value while pointer contains the memory address where the variable/value is

Jade
  • 2,972
  • 1
  • 11
  • 9