Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are object variables declared with a star

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?

like image 889
stone Avatar asked Dec 15 '10 19:12

stone


1 Answers

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.

like image 138
Marplesoft Avatar answered Nov 01 '22 08:11

Marplesoft