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

Putting the * directly next to the variable name makes this clearer.

A final note: Pointers can be difficult to get your head around at first. Do not worry if you have not mastered these ideas yet. You will be working with them for the rest of the book, and they will make more sense each time you do.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset