10

This may seem like trivial question.

But why is that we have to use the asterisk symbol when declaring object variables

Like, we do

Car * mazda = [[Car alloc] init];

What's the importance of the asterisk, I mean the compiler already knows it's an object, I'm sure the compiler can be trained not to complain about it. But then again by omitting it, I get an error message "statically allocating instance of objective-c class NSObject" What purpose would that serve?

Abizern
  • 146,289
  • 39
  • 203
  • 257
stone
  • 841
  • 3
  • 16
  • 26
  • possible duplicate of [Why \[object doSomething\] and not \[*object doSomething\]?](http://stackoverflow.com/questions/2189212/why-object-dosomething-and-not-object-dosomething) – bbum Dec 15 '10 at 20:59

5 Answers5

12

The asterix is a qualifier to the Car variable that you declaring. It means that you are declaring a pointer to a Car rather than declaring a Car itself. The return value of the init function (and the alloc function for that matter) is a pointer to a Car, not a Car itself, therefore this is correct.

Marplesoft
  • 6,030
  • 4
  • 38
  • 46
  • Thank you, that makes a lot of sense. So the asterix is mandatory in every scenario right. Or what I'm asking is, in which scenarios don't we use the asterix. – stone Dec 15 '10 at 19:40
  • 2
    When you are talking about an actual `Car` (or whatever class you're dealing with), you don't need a `*`. When you are referring to a **pointer** to a `Car` (like in this case), you do. – Daniel DiPaolo Dec 15 '10 at 20:21
  • There are no scenarios where you don’t use the asterisk for objects in Objective-C. In this sense, it isn’t needed. However, all other pointer types in C and hence Objective-C use the asterisk – int *foo, for instance – so it’s there for consistency. – Jens Ayton Dec 15 '10 at 20:26
  • @Daniel DiPaolo: your comment is confusing. What do you mean? `[Car class]` is of course also an pointer. – nils Dec 15 '10 at 20:32
  • @nils: Daniel means that if you were dealing with the actual object -- i.e., not a pointer to the object, or an object allocated on the stack -- you wouldn't use an `*`. Of course, in Objective-C, you can't allocate an object on the stack, so the point is kind of moot. – mipadi Dec 15 '10 at 21:02
6

With * you declare a pointer. That’s like in C, which is a subset of ObjC. Without the * the variable would be statically allocated which is not possible for ObjC objects (because the size of the object couldn’t be determined at compile time).

Statically allocated variables are used for primitive C types like int or double

int number = 42;
nils
  • 628
  • 3
  • 8
4

Objective-C requires that all objects are dynamically allocated (i.e. on the heap). The error you're getting indicates that you're trying to create a Car object on the stack. By declaring mazda to be a pointer to Car (Car*) rather than a Car, you satisfy that requirement.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • Thank you,In what scenario do I not use the asterix? – stone Dec 15 '10 at 19:45
  • 3
    If the variable in question is an Objective C object, you will always use the star. If the variable is is C-struct or primitive (NSInteger, CGRect, CGFloat, int, etc), you do not need to use the star, because C is fine with creating objects on the stack. – Matt Wilding Dec 15 '10 at 20:19
1

This is effectively a dupe of Why [object doSomething] and not [*object doSomething]?, which has a ton of background information.

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359
0

The asterix in variable declaration also means the value of the variable will not have a static allocated value. Its a pointer just like the others have said.

Boydo
  • 1