|Lessons||Lesson 25: Pointers to Pointers|
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.
NEXT>>>>>Lesson 26. String Manipulation