In this lesson, you’ll learn how pointers work in the context of C programming. The code example for this lesson will be in C, so you’ll see how creating a variable and assigning a value to it works differently than it does in Python. You’ll also cover the differences between passing by value and passing by reference.
Pointers in C
00:00 In this video, I’m going to explain the concept of pointers in the context of C programming. That means, for the time being, forget all about Python. You’ll learn about how this applies to Python in the rest of this course.
What we’re really doing here is telling C to allocate four bytes of memory to store an integer. Then we store the integer 123 in that memory space. C then assigns the name
x to that space in memory.
C code needs to be compiled and linked before it can be run as an executable. To do that, I’ll use what’s called a makefile, which allows me to obtain an executable just by running the
make command in the terminal.
This function will take an integer variable and then increment its value by 10.
void means that the function returns no data. In the main function, I call
change_variable, passing in our
x variable, which stores the value 123.
And it looks like we’re still seeing 123. It’s as if our new function didn’t do anything at all. What really happened is this: we created a new integer variable, then we passed it to the
At this point, the
change_variable function ends, and with it, our input variable here, with a value of 133, control returns to the
main method, and because we never actually changed the original four bytes of memory containing 123, that’s what gets printed.
05:17 This is fine for a simple four-byte integer, but think about what would happen if this were a variable that used hundreds of bytes of memory. If we pass that by value to a bunch of different functions to manipulate and use, then we’d be copying hundreds of bytes of memory over and over again, which is very computationally expensive and might create problems if we’re running this program on something like a microcontroller, with very little memory.
05:47 Instead, I’ll modify this program to do what’s called passing by reference. To do that, I’m first going to undo the recent changes I made. Now this function won’t return anything, just like we had it before.
That means that input is no longer the value 123. Instead, it’s the memory address to wherever 123 lives in memory. In order to actually access the data at the provided memory address, I’m going to append another
* (“star”) to the beginning of the variable name right here.
All that’s left to do now is modify the code inside the
main function. In its current state, the code won’t actually work. And that’s because we’re supplying the value 123 to the
change_variable function, which is asking for a memory address, not an actual value.
So let’s give it a memory address instead. We can do that by using the reference operator, which is the
& (ampersand). Appending this at the beginning of a variable will get the memory address of that variable’s value, instead of the value itself.
Notice how we don’t need to return anything here. That’s because we’re now passing
x by reference, so we’re allowing the
change_variable function to change the value stored at
x’s memory address directly.
08:11 Technically, we are still passing a copy of a pointer by value, which is eight bytes. This means that no matter how big the data pointed to by the pointer, we’ll only ever have to pass eight bytes of memory between functions.
08:42 There we go. Functionally, this looks the same as before, but now we’re passing by reference. There’s a lot more I can say about memory management and pointers, such as dynamic memory allocation, but this is all you need to know for this course.
Become a Member to join the conversation.