Advanced C Programming Pointers
Tags: C (programming language)
Master pointers, addresses and memory allocation in C
Last updated 2022-01-10 | 4.6
- Pointers and addresses- Indirection and multiple indirection
- Generic pointers and casts
What you'll learn
* Requirements
* You should understand at least the basics of C programmingDescription
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:
- computer memory and how pointers access it
- how memory is allocated
- why copying data using pointers can cause program errors
- why some pointers are ‘generic’
- what happens when you ‘cast’ pointers to specific types
- how to create singly and doubly linked lists
- how to use stacks and queues
- how to avoid memory leaks and other common problems
- ...and much more.
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.
Who this course is for:
- Any C programmer who needs to understand pointers in depth
Course content
4 sections • 69 lectures
What is this course about? Preview 02:44
Who this course is for and what you will get out of it.
How to use this course Preview 03:14
What is in the course and how should you study it?
Course Notes and FAQ Preview 00:27
Source Code Archive Preview 00:28
What is a pointer? Preview 03:38
Pointers and addresses - how are they related?
Pointer variables Preview 03:11
How to create and use a pointer variable in C.
Indirection Preview 01:51
Dereferencing to get at the data that is ‘pointed to’.
Pointer Basics
What are pointers?
Study Notes – Step One Preview 00:09
This document contains some brief notes on topics covered in Step One of the course. You may want to refer to use these notes as a revision aid or to help clarify important points from this section.
What is the relationship between a pointer and an array (or string)? Preview 03:46
The address of the array is the same as the address of the first item in the array. Why is that important?
How to display pointer values Preview 00:06
How can pointer values be displayed?
Arrays, addresses and pointers Preview 05:50
Let’s see how pointers and addresses work – and why arrays are special.
Multiple indirection Preview 01:41
Pointers to pointers
Multiple indirection with integers Preview 08:00
Pointers to pointers to integers
Multiple Indirection with strings Preview 04:57
Arrays of arrays of characters
Indirection and commandline args Preview 03:20
What is **argv in the main() function?
Generic Pointers Preview 04:44
Pointers to void
Allocating memory Preview 05:40
Setting aside storage space
Malloc and sizeof Preview 04:44
How to allocate the right amount of memory for a specific amount of data
Functions that cause errors or warnings Preview 02:49
Why does your C compiler will complain about some functions?
calloc Preview 04:22
How to clear memory before allocating
free Preview 03:07
How to free memory that is no longer needed
realloc Preview 04:03
How to change the size of a block of allocated memory
Pointer arithmetic Preview 05:03
What does it mean when you add 1 to a pointer?
Calculating an array index Preview 03:36
Using pointer arithmetic with arrays
Pointers to structs Preview 06:47
But how much memory does a struct need?
Data type alignment Preview 08:45
Why the order of fields in a struct is important
Type alignment on boundaries Preview 02:06
Understanding that data types of a certain size are aligned on boundaries of that size
Type alignment and pointer arithmetic Preview 02:16
Pointer arithmetic works with complex types as well as simple types
Debugging C Programs Preview 11:17
Still confused by pointers? Here I show how to use your debugger to understand indirection
Debugging Multiple Indirection Preview 08:18
Here’s another example of how to use a debugger with multiple indirection – pointers to pointers to pointers, and see how they are stored in memory
Addresses and Indirection
The relationship between pointers, addresses and data stored in memory
Study Notes – Step Two Preview 00:09
This document contains some brief notes on topics covered in Step Two of the course. You may want to refer to use these notes as a revision aid or to help clarify important points from this section.
Arrays and Lists Preview 01:26
Limitations of arrays and using linked lists as an alternative
What is a linked list? Preview 03:40
How is a linked list different from an array?
Singly linked lists Preview 04:55
Lists that include one pointer linking each element to the next one
To free or not to free? Preview 00:45
Do you really always need to free memory?
Doubly linked lists Preview 02:10
Lists whose elements contain pointers to each adjacent element
Programming a doubly-linked List Preview 03:20
An example showing how to create a simple double-linked list
Initializing a doubly-linked list Preview 01:10
Initializing a doubly-linked list not quite as simple as initializing a singly-linked list
Implementing a doubly linked list Preview 10:09
A sample project showing a doubly-linked list
What is a queue? Preview 02:35
Queues are very common data structures in programming – here I explain what they are
Queues Preview 04:23
Using a doubly linked list as a queue
What is a stack? Preview 02:10
A stack is another important type of data structure – here we see how it differs from a queue
Stacks Preview 02:27
You may need pointers with a stack, but you may not need a linked list
Pushing and popping Preview 05:05
Adding and removing items to and from a stack
Calculating the length of list Preview 03:15
Iterating over list elements
Copying a list Preview 04:02
Making a new copy of an existing queue
Removing an element from within a list Preview 06:17
How would you delete an item from the middle of a list?
Adding an element into a list Preview 04:49
How would you add an item into a list?
Function pointers Preview 01:04
What is a function pointer and what are they for?
Function pointer declarations Preview 03:38
The strange syntax for declaring a function pointer
An array of function pointers Preview 03:32
How a single piece of code can call different functions using function pointers
Lists and Data Structures
Pointers in linked lists, queues and stacks
Study Notes – Step Three Preview 00:09
This document contains some brief notes on topics covered in Step Three of the course. You may want to refer to use these notes as a revision aid or to help clarify important points from this section.
Why do pointers cause problems? Preview 01:39
Pointers are one of the major sources of error in C programs. But why?
Deep and shallow copies Preview 03:54
When is a copy not a copy?
A deeper look at deep and shallow copies Preview 07:31
Sometimes a copy may not be what you think it is!
Incorrect memory allocation Preview 01:56
Allocating the wrong amount of memory is a recipe for disaster
Casting pointers Preview 02:09
When you cast a pointer you are not really changing anything. But an incorrect cast can cause problems even so
Incorrect casts Preview 03:30
An example of a cast that didn’t work as expected
Freeing already freed memory Preview 03:06
What happens if you free some memory twice?
Memory leaks Preview 03:09
When memory is allocated but not freed
Using freed memory Preview 02:13
What happens when you try to use memory that has already been freed?
Pointers out of scope Preview 04:12
Trying to access things that can’t be accessed
Dereferencing a null pointer Preview 01:40
What happens if you use a pointer that doesn’t point to anything?
Common pointer problems
Some common sources of error in C programs.
Study Notes – Step Four Preview 00:09
This document contains some brief notes on topics covered in Step Four of the course. You may want to refer to use these notes as a revision aid or to help clarify important points from this section.
And finally... Preview 04:30
That’s it! So where to next? Here are a few pointers…