To be an expert C programmer you need to master the use of pointers. This course explains pointers in real depth. It explains pointer variables, pointer arithmetic, indirection, memory allocation, how to create and maintain linked lists and how to use function pointers. In fact, by the time you finish this course, you will know pointers inside out. You will understand what they are, how they work and how to make sure that they don’t make your programs crash!
This is not a course for beginners. It is aimed at programmers who already have a good working knowledge of C programming and who need to take the next step in mastering C by gaining a deep understanding of pointers.
If you’ve struggled with pointers and can’t quite figure out what all those ‘arrow diagrams’ really mean or what exactly is the relationship between pointers and addresses, this is the course for you. In a series of short, tightly-targeted lessons, you will learn all about:
The source code for all the example programs is provided, so if you need to try out my code you can load it and run it in your preferred C IDE or code editor.
Pointers and addresses - how are they related?
How to create and use a pointer variable in C.
Dereferencing to get at the data that is ‘pointed to’.
What are pointers?
The address of the array is the same as the address of the first item in the array. Why is that important?
How can pointer values be displayed?
Let’s see how pointers and addresses work – and why arrays are special.
Pointers to pointers
Pointers to pointers to integers
Arrays of arrays of characters
What is **argv in the main() function?
Pointers to void
Setting aside storage space
How to allocate the right amount of memory for a specific amount of data
Why does your C compiler will complain about some functions?
How to clear memory before allocating
How to free memory that is no longer needed
How to change the size of a block of allocated memory
What does it mean when you add 1 to a pointer?
Using pointer arithmetic with arrays
But how much memory does a struct need?
Why the order of fields in a struct is important
Understanding that data types of a certain size are aligned on boundaries of that size
Pointer arithmetic works with complex types as well as simple types
The relationship between pointers, addresses and data stored in memory
Limitations of arrays and using linked lists as an alternative
How is a linked list different from an array?
Lists that include one pointer linking each element to the next one
Do you really always need to free memory?
Lists whose elements contain pointers to each adjacent element
An example showing how to create a simple double-linked list
Initializing a doubly-linked list not quite as simple as initializing a singly-linked list
A sample project showing a doubly-linked list
Queues are very common data structures in programming – here I explain what they are
Using a doubly linked list as a queue
A stack is another important type of data structure – here we see how it differs from a queue
You may need pointers with a stack, but you may not need a linked list
Adding and removing items to and from a stack
Iterating over list elements
Making a new copy of an existing queue
How would you delete an item from the middle of a list?
How would you add an item into a list?
What is a function pointer and what are they for?
The strange syntax for declaring a function pointer
How a single piece of code can call different functions using function pointers
Pointers in linked lists, queues and stacks
Pointers are one of the major sources of error in C programs. But why?
When is a copy not a copy?
Sometimes a copy may not be what you think it is!
Allocating the wrong amount of memory is a recipe for disaster
When you cast a pointer you are not really changing anything. But an incorrect cast can cause problems even so
An example of a cast that didn’t work as expected
What happens if you free some memory twice?
When memory is allocated but not freed
What happens when you try to use memory that has already been freed?
Trying to access things that can’t be accessed
What happens if you use a pointer that doesn’t point to anything?
Some common sources of error in C programs.
That’s it! So where to next? Here are a few pointers…
Huw Collingbourne is the technology director at SapphireSteel Software, developers of programming tools for Microsoft Visual Studio. He is author of The Book Of Ruby from No Starch Press. He runs Bitwise Courses and teaches courses on a range of programming topics.
Huw has been a programmer for more than 30 years. He is a well-known technology writer in the UK. For over ten years he wrote the Delphi and Java programming column for PC Plus Magazine. He has also written numerous opinion and programming columns (including tutorials on C#, C++, Smalltalk and Ruby) for a number of computer magazines, such as Computer Shopper, PC Pro, and PC Plus.
In the 1980s he was a pop music journalist and interviewed most of the New Romantic stars, such as Duran Duran, Spandau Ballet, Adam Ant, Boy George, and Depeche Mode. He is now writing a series of New Romantic murder mysteries.
At various times Huw has been a magazine publisher, editor, and TV broadcaster. He has an MA in English from the University of Cambridge and holds a 2nd dan black belt in aikido, a martial art which he teaches in North Devon, UK. The aikido comes in useful when trying (usually unsuccessfully) to keep his Pyrenean Mountain Dogs under some semblance of control.