Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of const keyword positioning in variable declarations

What is the significance of the positioning of the

const

keyword when declaring a variable in Objective-C, for example:

extern const NSString * MY_CONSTANT;

versus

extern NSString * const MY_CONSTANT;

Using the first version in assignments produces warnings about "qualifiers from pointer target type" being discarded so I'm assuming that the second version ensures that the pointer address remains the same. I would really appreciate a more definitive answer though. Many thanks in advance!

like image 443
Andy Bowskill Avatar asked Jan 17 '10 21:01

Andy Bowskill


1 Answers

In the first case, you are declaring a mutable pointer to an immutable const NSString object, whereas in the second case, you are declaring an immutable pointer to a mutable NSString object.

An easy way to remember this is to look at where the * is situated; everything to the left of it is the "pointee" type, and everything to the right of it describes the properties of the pointer.

like image 135
ezod Avatar answered Sep 19 '22 13:09

ezod