03 – (NSString **) value

Java developer are familar with references. But sometimes they are not with pointers. But what is the difference. Isn’t a reference a pointer too?

Java does not make any difference between a reference and a pointer. If you keep a reference to an object, this reference represents the object which is the target of the reference. If you try to get a reference to this reference, you will fail using java, because java does not offer any semantics to access references on references.

Objective-C does. It allows you to access references on a reference and to deal with this reference. Additionally you can use arithmetics on this references. For instance you can add an integer value to a reference which will cause the reference to point to a different memory adress. By definition a pointer is a “variable which contains the adress in memory of another variable”.

Do make this circumstance more visual, have a look at the following image.

The variable charPtr is a variable containing a char-pointer (blue arrow) which points to a charValue.

Actually you can two different interesting things with the variable charPtr.

  • change the value the pointer is pointing to
  • assigning a different pointer
    // Helper variables
        // value of the char
        char val = 'someChar';
        // accessing the pointer to the char
        char *valPtr = &val;

    // change the value the pointer is pointing to. See the red X (char) at the image
        *charPtr1 = 'X';          // changing value of pointer
    // Assign a different pointer. See red arrow at the image
        charPtr1 = valPtr;        // changing adress of pointer

 

During the study of objective c you might have recognized the additional “*” for variable declarations.

  NSString *value

Variable declarations in java do not contain such a *

  String value

This * tells the compiler, that this variable is a reference / a pointer.

Lets try creating a pointer to a pointer.

  NSString **valuePtr

This variable represents a reference to a reference which’s target is the NSString value.

You can define this pointer also that way:

NSString** valuePtr

I am telling you this case, since it was a very helpful mnemonic for me. It can be very confusing if you are accessing values from pointers.

For me it was much more intuitiv setting the stars at the end of the type, rather then setting them to the beginning of the variable to define a new variable.

Using such **variables let you do really amazing things which are not possible using java.

For instance you can implement a get-method without a return value. Rather then returning a reference to the value, change the reference of the reference itself.

A try with java:

         ......
           String name = "NotFlorian";
           getName(name);
           // name is still NotFlorian!!!
         ......

          public void getName(String name){
                name = "Florian";
          }

This approach won’t change the value of the passed “String name”, because the method only changed the value of the local variable “name”. Since the local variable and the passed variable have different locations in memory, changing the local variable could not change the passed one.

A try with Objective-C:

         ......
           NSString *name = @"NotFlorian";
           [self getName:&name];
           // name is Florian!!!
         ......

         -(void) getName:(NSString **) valuePtr{
	      *valuePtr =  @"Florian";
         }

The Objective-C approach work really well! The big difference to the java approach is, that we did not pass a instance pointer (reference to a variable) but a reference to the instance pointer into the getName-method. The assignment changed the target of the reference pointer and so the *name variable in the calling code changed to the new value.

&name adresses the pointer of the reference.

Here an example for pointers to pointers to instancePointer.

	NSObject *instancePtr = [[NSObject alloc] init];
	NSObject **ptrToInstancePtr = &instancePtr;
	NSObject ***ptrToPtrToInstancePtr = &ptrToInstancePtr;

Good… I think that is enough about the difference of objective c pointers and java references. Objective c pointers are much more powerful than java references, since c allows the developer to deal with pointers directly.

Advertisements

Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: