Like other variables, you can create an array of pointers. Each element in a pointer array references a memory location, like a collection of strings in an array. The advantage of creating an array of pointers is that you merely need to swap two elements as opposed to copying the data chunks—such as strings—the pointers reference.
- [Instructor] Like any variable you can have an array of pointers. The best way to describe how such a beast might look is with an array of strings. So in this exercise file veeps is an array of five pointers. Each pointer is initialized to a string, the last names of the first five vice presidents of the United States. The compiler allocates storage for each string, those addresses are then available as elements in the veeps array, output at line nine using this array notation, build and run. And here you see the strings output. This method of allocating an array of strings is far more memory efficient than creating a two-dimensional character array, plus it has other advantages as well. In this modification of the code I've replaced the array notation at line nine with pointer notation. So at the base of the array is veeps, which acts like a pointer and then x is an offset representing the element of the array which is an address. The asterisk fetches the value at that address which is each of the strings. The output should be the same as the preceding example, and it is. So what's the advantage to an array of pointers? Well if you need to swap two strings, you just swap their addresses, not every single character in each string. So in this exercise file I swap misters Jefferson and Burr. A temp pointer declared at line six helps swap the two array elements, the addresses, which takes place at lines 10 through 12. Build and run. And you see that Jefferson and Burr are reversed in the output. Remember all that was swapped here were the addresses not all the characters in the strings. In this exercise file I get kind of crazy outline nine, the dreaded double asterisk notation appears. Now do not freak out, it's a pointer to pointer address. So already you know that veeps is an array of pointers. But each pointer itself in the array references a string. So let's add some parentheses to kind of unravel what's going on here, and I need three over here which the editor automatically assumes for me, so be careful. And and I'll read this thing from the inside out. So veeps is the base of the array and then when you add X to that you get each element so each address in the array. The asterisk represents the string, but the string is also a base address. So if you take the asterisk for that base address, what's returned is a character, and the character is output by the printf statement. See how this works? And there you see the first letter of each name in the array. In this modification I'm adding plus one to that concoction. Remember address veeps plus x is an address. If we add one to that address we get the next memory location, which should be the second character in the string . And then the asterisk fetches that character from this second location in the string. See how that works? And there's the second letter of each name. Now you can go nuts with pointers to pointers. And they're necessary in some circumstances. To help you understand them unravel the expression, talk through it and that way you'll get a handle on what's going on.
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions