views:

58

answers:

3

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 !

A: 

Are you sure it acts differently ? It seems a bit strange to me. Even if I don't know about Objective-c, in C there would be no difference between

Type* var
Type * var
Type *var

It's just a matter of syntax / preference.

Except if you're not talking about declaring a variable, but access it ?

Guillaume Lebourgeois
+3  A: 

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’”.

Konrad Rudolph
+1  A: 

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; 
Vladimir