Q&A with Richard Reese, author of "Understanding and Using C Pointers"
Q. Why is your book timely?
A. The C language continues to be a popular and widely used language. In addition, there are millions of lines of C code being maintained. Pointers are central to C and a solid understanding of them is important in crafting reliable and secure software. Understanding C pointers has always been important but they have not always been adequately covered. Their widespread usage in new and old applications begs a complete and comprehensive coverage.
Q. What do you hope that readers of your book will walk away with?
A. I hope that the experienced developer will be able to fill any holes in their understanding of C pointers. I hope that the less experienced developers will become more proficient and confident in their application of pointers.
Q. Can you give us a few tips on using pointers?
- Be careful when using pointers to void. If you cast a pointer to a pointer to void there is nothing preventing you from casting it to a different pointer type.
- When using function pointers the programmer must be careful to ensure it is used properly because C does not check to see if the correct parameters are passed.
- New code development should use the restrict keyword with most pointer declarations. This will enable better code optimization. Modifying existing code may not be worth the effort.
- Compilers do not always do a good job at reporting alias-related warnings. They can sometimes miss aliases and may sometimes report alias problems where they don't exist. It is ultimately up to the programmer to identify alias conditions.
- Remember NULL and NUL are different. NULL is used as a special pointer and is typically defined as ((void*)0). NUL is a char and is defined as '0'. They should not be used interchangeably.
- Improperly aligned pointers can result in an abnormal program termination or retrieval of bad data. In addition, slower pointer access is possible if the compiler is required to generate additional machine code to compensate for the improper alignment.