Pointers to Pointers
What we have been talking about, now we will introduce a new terminology, is actually a case of ‘Pointer to Pointer’. We were doing double dereferencing to access the elements of a two-dimensional array by using array name (a pointer) to access a row (another pointer) and further to access a column element (of ‘int’ data type). In case of single dereference, the value of the pointer is the address of the variable that contains the value desired. In the case of pointer to pointer or double dereference, the first pointer contains the address of the second pointer, which contains the address of the variable, which contains the desired value. Pointers to Pointers are very useful. But you need to be very careful while using the technique to avoid any problem.
We can also declare an array of pointers. An array of pointers is used to store pointers in it. Now we will try to understand how do we declare an array of pointers. The following statement can help us in comprehending it properly.
char * myarray;
We read it as: ‘myarray is an array of 10 pointers to character’.
Command Line Arguments
Until now, we have always written the ‘main()’ function as under:
But we are now in a position to write something inside the parenthesis of the ‘main()’ function. In C language, whenever a program is executed, the user can provide the command-line arguments to it like:
C:\Dev-cpp\work>Program-name argument1 argument2 ……argumentN
We have so far been taking input using the ‘cout’ and ‘cin’ in the program. But now we can also pass arguments from the command line just before executing the program. For this purpose, we will need a mechanism. In C, this can be done by using ‘argc’ and ‘argv’ arguments inside the main( ) function as:
void main(int argc, char **argv)
Note that ‘argc’ and ‘argv’ are conventional names of the command line parameters of the ‘main()’ function. However, you can give the desired names to them.
argc = Number of command line arguments. Its type is ‘int’.
argv = It is a pointer to an array of character strings that contain the arguments, one per string. ‘**argv’ can be read as pointer to pointer to char.
Now the command line arguments can be accessed from inside the program using ‘argc’ and ‘argv’ variables. It will be an interesting experience for you to try out the following code:
/* Accessing the command line
If we run this program without any argument, then what should be the answer. It will be not correct to think that the argc (number of arguments) is zero as we have not passed any argument. It counts program name as the first argument. So programs written in C/C++ know their names supplied in the first command-line argument. By running the above program, we can have the following output:
Here we see that the number of arguments is 1 with the first argument as the program name itself. You have to go to the command prompt to provide the command line arguments.
The command line arguments are separated by spaces. You can provide command line arguments to a program as under:
c:\dev-cpp\work>program 1 2
Here the number of arguments (argc) will be 3. The argument “1” and “2” are available inside the program as character strings.