Many “freshman” programmers do not fully understand or appreciate pointers in the C language. When I wanted to learn about pointers— variables that contain a memory address— I took a book on summer vacation and could hardly wait to get back to my PC to try example programs. Pointers were cool and versatile. Since then I have used pointers infrequently except for a recent project that requires a function to operate on an array. You don't pass an entire array to a function. Instead you use a pointer to locate the array and a value to indicate the number of elements. Frankly, I could program what I needed these days without including pointers in my code, so I hardly gave them a thought.
So I got quite a surprise when I saw the book, Understanding and Using C Pointers on the new-arrivals shelf at our county library's local branch. I checked it out and took it home. Although I have gotten only part way through and have skipped a few sections, I find the information easy to follow and replete with example code. Better yet, the author included many diagrams that show memory allocations, pointer connections between memory sections, and the use of pointers in helpful examples. I'll buy a copy of this book and continue reading.
The book starts with an introduction in which the author, Richard Reese, explains why C programmers should know how to use pointers. Those reasons include creating fast and efficient code, the capability to pass large amounts of information to functions with an address rather than a lengthy section of code that moves data, and the dynamic allocation of memory. For me, the four chapters that explain how to use pointers with functions, arrays, strings, and structures provide the most important information and examples for programmers and engineers. Chapter 7, “Security Issues and the Improper Use of Pointers,” looks interesting, but I haven't reached it yet. You might recall many hacking attacks took, or take, advantage of pointers used improperly in software.
You might think, “I deal mainly with code for hardware, so pointers seem unimportant.” But you might need them sometime, and you might need to understand how they work when you encounter them in another person's code:
void allocateArray(int *arr, int size, int value)
arr = (int*)malloc(size * sizeof(int));
Hmmm. What are all those asterisks for?
Reese, Richard, “Understanding and Using C Pointers,” O'Reilly Media, Sebastopol, CA, USA. 2013. 208 pages, paperback. ISBN: 978-1-449-34418-4. List price: $29.99.
For more information, please visit this page.
This article was previously published on Embedded.com’s sister publication, EDN Magazine.