www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 25: Pointers to Pointers Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
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[10];

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:

main( )
{
. . . // code statements
}

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 arguments */
#include <iostream.h>
main(int argc, char **argv)
{
cout << argc << endl;
cout << *argv;
}

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:

c:\dev-cpp\work>program
1
program

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



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.