Recommend viewing at 720p HD full screen. Post any questions or comments in the discussion section below. To view a modified text transcript of this lesson, click here.
Now, everything should be clear about pointers concerning everything we have covered so far with one exception: arrays. Here we are going to finish this review by studying arrays as they relate to pointers.
Let's consider this code:
char my_string="Hello Reddit";
Ok, we now have a pointer called my_pointer. We want it to "point to" our array. What does this mean? It could actually mean several things.
Most likely, it means we want to create a pointer that will contain the memory address where our array begins, and it would be used to read one character of the array at a time.
This is the first example we will look at, as it is the simplest. In this case, from the above code, there are two ways to do this.
First, I can say this:
my_pointer = my_string;
Why? because my_string is already understood by C to be a memory address. Any array name is seen as a memory address. More specifically, it is seen as the memory address of the exact start of the array.
Another way we can achieve the exact same goal, is like this:
This is saying, "Assign my_pointer the memory address of the first character in the array my_string".
It makes sense right? If we wanted to set my_pointer to be equal to the second character, we would write: my_pointer = &my_string; Consider that my_string is the first element of the array. Therefore, &my_string should be the memory address of the first element of the array. So this much should make sense to you.
These two examples are the same. We can set a pointer to contain the memory address of the first element of an array which means exactly the same thing as setting the pointer to contain the memory address to the start of the array itself. Why? Because the start of the array is the first element.
So both of these lines of code achieve the same goal: