Join Dan Gookin for an in-depth discussion in this video Accessing arrays with pointers, part of Learning C (2014).
- Where pointers really come into play, is when dealing with arrays. After all, there is no such thing as an array in the C language. All arrays are simply shorthand for pointers. To prove it, in this movie I dissect the common C language array. I'll show you how pointers can be used to manipulate an array, providing flexibility and power other programming languages can only dream about. The code here is pretty basic. An array is declared, and its value is assigned.
A for loop marches through each element, displaying that elements value (mumbling). It's so easy, I'm not even going to run it. Open exercise file array pointer two. This is pretty much the same code, but a pointer is used to display the arrays values. The A pointer variable is declared at line seven. It's initialized at line nine. An ampersand isn't needed here, because as I said in an earlier movie, arrays are all shorthand for pointers.
So, the array name is really a memory location. In the for loop, the APTR variable is used with the asterisk, so that the value at the memory location is fetched. During the loops first (interaction), the value is the same as the zeroth element in the array. Look at line 14. The APTR pointer variable is incremented. In this format, the pointer variable is a memory location.
So, the memory location is incremented, but by how much? APTR is an integer pointer. The memory address it holds is incremented by the size of an integer variable. The storage space that variable uses in memory. Conveniently, that happens to be the location of the next element in the array. Build and run the code. It works. The output is the same as it would have been for the previous exercise how'd you bother to build and run that code.
The pointer variable is used to march through the array. The advantage here, is that pointers can be used to manipulate array data and pointers are variables. Go back to the first source code file, array pointer one. Add a new line below line six, to create the pointer variable. Initialize the variable as was done in the other exercise. Now the tricky statement.
Set the value for the third element to zero by using the pointer variable. The pointer variable is all ready referencing the first element in line nine. To reference the third element, you need to add two to its value. To assign that memory location the value zero, you use the asterisk operator. The rest of the code can remain the same. Save.
Build and run. The third element is now zero. It has been manipulated by using a pointer. Most C language programmers will combine the statements at line 10 and 11 into a single line. It looks like this. You need to use parenthesis, because the pointer location math must happen before the asterisk references the value. Save the changes. Build and run.
The output is the same. You'll see this type of shorthand notation used a lot. The main reason is that it doesn't effect the value of the APTR pointer variable, which still references the base address of the array. Perhaps, the best way to demonstrate pointer and array manipulation is to use strings instead of numeric arrays. Open exercise array pointer three. In addition to declaring a string as a character array, you can also declare a string as a char pointer variable.
The compiler makes the assignment, but the variable created is a pointer. In this code, the pointer variable is used just like an array name here in the puts function. Build and run to confirm that this approach is not crazy. Now, this begs the question that if you're afraid of pointers, how would you display this string one character at a time. Open exercise array pointer for. Yes, this is the chicken way to do it.
It works, because array notation is simply shorthand for pointers. In fact, many C programmers would do exactly as you see here, which is to use array notation to display the string. Build and run. It works, but this approach is not why you're taking this course. I'm going to replace the array notation with pointers. All you need is only one pointer, which I'll call PTR, and you don't need an X.
No ampersand is needed here, because string is all ready a pointer. It holds a memory location. To examine a character at a memory location, the asterisk operator is used. So, the while loops condition becomes that's it. Ditto for the put care function. Then you increment the memory location, which is. So, these two represent characters, and this represents a memory location.
Marching through the string. Save the changes. Build and run. The output is still the same. Now, you could go one step further if you'd like and not even use the PTR variable. I'll make the necessary modifications in the code. Save. Build and run. The issue here is that once you change the string variable, you loose its base location in memory. For this code that's not an issue, but for other situations it could present a problem.
- Understanding a C program
- Adding comments
- Using escape characters
- Working with values and placeholders
- Introducing variables
- Making a decision with if
- Adding functions
- Manipulating strings
- Building arrays
- Nesting structures