Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In objective-C what's the difference between Type* var, and Type *var?

Oh the joys of being a memory management noob !

I'm getting bit by some Objective-C code, eventhough I understand the basics of pointers, I've seen these two different constructs, without being able to really understand their difference.

Can anyone enlighten me ?

Edited my question, the constructs didn't behave differently, instead I got bit yet again by the multiple declarations gotcha. Thanks !

like image 504
julien Avatar asked Feb 03 '26 05:02

julien


2 Answers

There’s no difference – it’s a matter of taste. However, beware that the pointer actually always binds to the name, not the type. So this:

Type* var1, var2;

Declares var1 as a pointer to Type, while var2 is not a pointer. That’s just one more reason not to declare multiple variables in the same statement.

Historically, the Type *var notation is more common in C, where it is read as “var is declared as a pointer to Type”, i.e. “the type of *var is Type”. In C++, on the other hand, Type* var is more common and is read as “var is declared as being of type ‘pointer to Type’”.

like image 84
Konrad Rudolph Avatar answered Feb 05 '26 21:02

Konrad Rudolph


There is no difference at all.

But consider the following line:

Type* var1, var2;

Here only var1 is a pointer to Type, but it is easier to see that if you write

Type *var1, var2; 
like image 29
Vladimir Avatar answered Feb 05 '26 23:02

Vladimir