Arrays of Pointers

So far you have used only single pointer variables, but you can also use arrays of pointer variables if you need to store a series of addresses. One such need for an array of pointers is when dealing with a series of text strings (character arrays). This will be demonstrated using the command-line arguments that a C program can receive when it is started.

When you run a compiled C program, you can optionally add a number of arguments that are passed to that program. From a command-line interface, arguments are separated by one or more blank spaces, and come after the application name (Figure 9.15). In this figure, pointer9 is the program name and aaa and bbb are two separate arguments.

Figure 9.15. C programs can receive a variable number of command-line arguments. The arguments are separated by white space.


How can your C program access these arguments when it may not now how many there are? C always passes two parameters to your main function: the first one is an integer with the number of arguments the application received, and the second one is an array of pointers to where those arguments have been stored in memory. By convention, the first parameter is named argc (for argument count) and the second one argv (for argument vector, where vector means array). As usual, each string (which is to say each argument) is a character array, terminated with a null byte. Note that there is always at least one argument because C inserts the program name at the front of the list of arguments (Figure 9.16).

Figure 9.16. C stores the command-line arguments, including the name of the program itself, as character arrays.


Let's write a program that prints the arguments given to the program and displays the memory locations for the different objects shown in Figure 9.16.

To use program arguments

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 9.8):

/* pointer8.c - Script 9.8 */
#include <stdio.h>

Script 9.8. C programs can access their command-line arguments through an array of pointers to strings.


3.
Begin the main function:

int main(int argc, char *argv[]) {

This time, the main function is not defined to take no arguments (using void). Instead, it's defined to take the two arguments explained earlier: char *argv[] is the equivalent of saying “argv is an array (of unknown size) of pointers to character variables.”

4.
Define and initialize an integer i to be used as loop index variable:

int i;

5.
Print the argument count:

printf("Number of arguments received: 
 %d
", argc);

When the program runs, the argc variable will be assigned the number of arguments the application received. This value is first printed.

6.
Print the base address of the argv array:

printf("argv base address: %p

", 
 argv);

Using the information we presented in the previous section of this chapter, the array's base address—the location where the array's first element is stored—is printed.

7.
Add a loop that prints out the addresses and values of each received argument:

printf("Address --> Value
");
for (i = 0; i < argc; i++) {
    printf("%p —> %s
", argv[i],
 argv[i]);
}

The loop goes from 0 (the first item in the argv array) to one less than the number of arguments received (argc). For every element in the argv pointer array, this loop will first print the actual address stored in that element using the %p sequence. Then the character string that starts at that address will be printed as a string. This last value corresponds to the actual command-line argument.

8.
Complete the main function:

    getchar();
    return 0;
}

9.
Save the file as pointer8.c, compile, and debug as necessary.

10.
Run the application, providing it with some number of arguments (Figure 9.17).

Figure 9.17. The application displays the base address of the argv array and the addresses and pointed values stored in the elements of the array.


✓ Tips

  • You can experiment by running the compiled program with different argument counts and lengths. The dynamic, pointer-based system will work in any situation.

  • Most graphical programming environments do not run applications from a command line. In such cases, you have to find out how to pass arguments to your program when running the program. In Apple's Xcode for example, after successful compilation, click on the compiled program's name in the Executables folder. Then, select View > Show Embedded Editor. There, under the Arguments tab, you can add the arguments (Figure 9.18).

    Figure 9.18. Specifying command-line arguments in Xcode.

  • In Dev-C++ on Windows, first compile the application. In the resulting window, click on Parameters to display the parameters box. Type any arguments there (Figure 9.19), then click on Execute.

    Figure 9.19. Specifying command-line arguments in Dev-C++.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset