Stylish pointer declarations

When you declare a pointer to float, it looks like this:

f​l​o​a​t​ ​*​p​o​w​e​r​P​t​r​;​

Because the type is a pointer to a float, you may be tempted to write it like this:

f​l​o​a​t​*​ ​p​o​w​e​r​P​t​r​;​

This is fine, and the compiler will let you do it. However, stylish programmers do not.

Why? You can declare multiple variables in a single line. For example, if you wanted to declare variables x, y, and z, you could do it like this:

f​l​o​a​t​ ​x​,​ ​y​,​ ​z​;​

Each one is a float.

What do you think these are?

f​l​o​a​t​*​ ​b​,​ ​c​;​

Surprise! b is a pointer to a float, but c is just a float. If you want them both to be pointers, you must put a * in front of each one:

f​l​o​a​t​ ​*​b​,​ ​*​c​;​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.