Does it make sense to attribute where you put the pointer pointer "*" in Objective-C?

Here's a confusing simple question that I couldn’t find the answer to when I delve into Objective-C:

Is there any sense attributed to where you indicate the pointer pointer '*' when declaring or setting a variable?

I saw variables defined in different ways:

NSString *BLANK_SPACE = @" "; NSString const *BLANK_SPACE = @" "; NSString * const BLANK_SPACE = @" "; 

Now I know the meaning of the CONST modifier, but I put it there only because when I see an asterisk separated by a space, it is usually before the CONST modifier.

Can someone explain the rationale for where to put * when declaring / assigning a variable? What is the difference?

+7
source share
3 answers

const is a postfix operator that refers to a thing to the left of it, and not to the right. If const appears in the first step, then this refers to the first thing after it. const NSString *foo (as well as NSString const *foo ) means that it is a non-constant pointer to const NSString - the pointer value can be reassigned, but the pointer data is unchanged. NSString * const foo means that it is a const pointer to a non-const NSString - the data pointed to data can change, but the location that the pointer refers to cannot.

The interval between * and other parts of the line is only a matter of style and clarity.

+9
source

The trick is to read declarations from right to left. So:

 NSString *BLANK_SPACE 

From right to left ... BLANK_SPACE is a pointer to an NSString.

 NSString const *BLANK_SPACE 

From right to left ... BLANK_SPACE is a pointer to const NSString.

 NSString * const BLANK_SPACE 

From right to left ... BLANK_SPACE is a constant pointer to NSString.

Finally,

 NSString const * const BLANK_SPACE 

From right to left ... BLANK_SPACE is a constant pointer to const NSString.

+2
source

The * position relative to the variable name does not matter to the compiler. Some people prefer to put * with a variable, though, because it avoids confusion when declaring multiple variables:

 char* a, b; // a is char* and b is char, but both look like char* char *a, b; // looks a little more like the truth: a is char*, b is char 

However, you always reference objects in Objective-C through pointers, so never do something like the first line below:

 NSString *a, b; // WRONG: a is a pointer to an NSString object, b is just wrong. NSString *a, *b; // OK: both a and b are pointers to NSString objects 

On the other hand, the position of const really matters:

 int * const a; // a points to an int, and a can't be modified to point to some other int int const * a; // a points to an int, and that int can't be changed 

Now const does not make much sense regarding objects. First, NSString represents an immutable string, so declaring one const does not add much. For the other, as a rule, it modifies the object in Objective-C by sending a message, rather than directly changing the ivars object, and I don't think the compiler will prevent changes made through messages. Therefore, a const pointer makes sense, a pointer to a const object, not so much:

 NSString * const BLANK_SPACE = @" "; // makes sense; pointer BLANK_SPACE can't change NSString const * BLANK_SPACE = @" "; // not useful 
+1
source

All Articles