0

I'm reading a book called Java and C: Key Differences, Learn Objective-C for Java Developers. (my background is C#/Java so the concept of pointers and managed memory is new to me, so please bear with me)

In chapter 2, it explains pointers with this example:

int i = 1;
int *iptr;
iptr = &i;
*iptr = 2;

I understand the above, where *iptr is a pointer, it is assigned the memory location of i, also from the pointer iptr we can access the value iptr points to with the *iptr syntax.

Further down the book there is the following snippet:

//..    
NSMutableString *z = [[[NSNutableString alloc]init] autorelease];
[z appendString:@"Zombie "];
//...

From my understanding of chapter 2, the line [z appendString:@"Zombie "] should have been [*z appendString:@"Zombie "] instead, as we want the actual NSMutableString, not to its pointer, to be sent the message appendString!? I know I'm wrong and there is something that I'm missing, please point me to the right direction.

Many thanks,

Giuseppe

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Giuseppe Romagnuolo
  • 3,362
  • 2
  • 30
  • 38
  • possible duplicate of [Why \[object doSomething\] and not \[*object doSomething\]?](http://stackoverflow.com/questions/2189212/why-object-dosomething-and-not-object-dosomething) – bbum Nov 01 '10 at 19:49
  • I wrote a rather long winded explanation of this in http://stackoverflow.com/questions/2189212/why-object-dosomething-and-not-object-dosomething/2214980#2214980 – bbum Nov 01 '10 at 19:50

4 Answers4

6

In Objective-C, you always work with pointers to objects (you can't create an object on the stack). Messages are always passed to pointers (although that's only an abstraction -- more complicated things are done at the runtime level).

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Almost -- Blocks are both Objective-C objects and they start on the stack. The real issue, though, is how objc_msgSend() is defined; it takes a reference to an object. – bbum Nov 01 '10 at 19:51
4

The simple answer is that the Objective-C syntax is not the same as plain C language.

Yes, an Obj-C object is defined with the normal C syntax since it's actually a pointer to a structure but is then used without the * operator while dealing with Objective-C instructions..

Actually nothing forbids the semantics of Objective-C from sending a message to an object through its pointer, so simply don't worry.

In any case this applies to every object but there are many typedefs (for example NSInteger) that wraps primitive o structured C types. They can be used in C either by directly working with them onto the stack, either by allocating them.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • This is a bit misleading. The declaration of objc_msgSend() -- the fundamental messenger in the language -- takes an object reference as first argument, hence the reason why the reference is used and not *foo. – bbum Nov 01 '10 at 19:52
2

Probably you are not aware that

NSMutableString *z = [[[NSNutableString alloc]init] autorelease];

is the abreviated form of

NSMutableString *z;
z = [[[NSNutableString alloc]init] autorelease];

Probably now is clearer. As mipadi said, in Objective-C you always deal with pointers to objects and not real objects (like sometimes in C++ for example)

nacho4d
  • 43,720
  • 45
  • 157
  • 240
1

The bracket notation [obj message] implies that obj is a pointer to an object. Thus * is to be expressed.

mouviciel
  • 66,855
  • 13
  • 106
  • 140