Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.

Similar presentations


Presentation on theme: "1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3."— Presentation transcript:

1 1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3 To achieve this, main() allows two input parameters to hold the command-line options. int main(int argc, char *argv[]) {return 0; } argc stores the number of parameters argv is an array. Each element of the array is a character pointer Computer Programming and Basic Software Engineering 6. Pointers and Arrays e.g. ipconfig /all You can also write *argv[] as **argv The values of argc and argv[] are provided indirectly

2 2 Command-Line Processing and Parameter Passing Computer Programming and Basic Software Engineering

3 3 6. Pointers and Arrays Example SomeProgram Param1 Param2 Param3 int main(int argc, char *argv[]) { // function statements here return 0; } argc = 4 argv[0] = "SomeProgram" argv[1] = "Param1" argv[2] = "Param2" argv[3] = "Param3" argv[0] = "SomeProgram" argv[1] = "Param1" argv[2] = "Param2" argv[3] = "Param3" main() is inside the project SomeProgram i.e. argv[0][0] is 'S'

4 4 Command-Line Processing #include using namespace std; int main(int argc, char *argv[]) { cout << "Received " << argc << " arguments." << endl; for (int i = 0; i<argc; i++) cout << "argument " << i << ": " << argv[i] << endl; return 0; } Computer Programming and Basic Software Engineering 6. Pointers and Arrays To add command-line options, this program must be run in command-line, not Start Without Debugging. Every command line options will be printed out The program can flow according to the command line options.

5 5 Exercise 6.2d Build the program in the last page with the project name Ex6p2d. Try to locate the executable file of the built program using the Windows Explorer. Open a Command Prompt to execute this program with the following command line: Ex6p2d aa bb param3 param4 What are shown on the screen? Try to input different number of command-line options to see the result. Computer Programming and Basic Software Engineering 6. Pointers and Arrays

6 6 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Exercise 6.2e For the program in Ex6.2d, modify it such that a warning message will be given if any two command-line options are the same.

7 7 6.3 Parameter passing using pointers Computer Programming and Basic Software Engineering 6. Pointers and Arrays

8 8 #include using namespace std; void swap(int x, int y) {int temp; temp = x; x=y; y=temp; } int main() {int x = 5, y = 10; cout << "Main. Before swap, x: " << x << "y: " << y << "\n"; swap(x,y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } Pass Parameters – Pass by Value We need to pass parameters to functions We may pass parameters by value, i.e. pass copies of the parameter values to functions. x and y are swapped only in swap() but not in main() Computer Programming and Basic Software Engineering 6. Pointers and Arrays

9 9 1. At main() Computer Programming and Basic Software Engineering 6. Pointers and Arrays 010001040108010c011001140118011c01200124 Address The stack Variables 510 xy x and y of main() 4. When swap() returns 510 xy x and y of swap() 2. When swap() is called pass-by-value 3. When x and y is swapped 105 xy x and y of swap() temp = x; x = y; y = temp;

10 10 Pass by Reference pointer allows pass parameters by reference. #include using namespace std; void swap(int *px, int *py) {int temp; temp = *px; *px=*py; *py=temp; } int main() {int x = 5, y = 10; cout << "Main. Before swap, x: " << x << " y: " << y << "\n"; swap(&x,&y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } The addresses of x and y in main() are passed to swap() Computer Programming and Basic Software Engineering 6. Pointers and Arrays

11 11 Computer Programming and Basic Software Engineering 6. Pointers and Arrays 010001040108010c011001140118011c01200124 Address The stack Variables 510 xy 1. At main() x and y of main() 01000104 pxpy px and py of swap() 2. When swap() is called pass-by-ref 105 temp = *px; //5 *px = *py; //10 *py = temp; //5 3. When *px and *py is swapped 01000104 pxpy px and py of swap() 105 4. When swap() returns

12 12 Return Multiple Values Normal function can only return 1 value If more than 1 values are to be returned, it can be done by passing two or more parameters to a function by reference. #include using namespace std; void opt(int, int *, int *); //prototype int main() {int num, sqr, cub; cout << "Input a number: "; cin >> num; opt(num, &sqr, &cub); cout << "Square = " << sqr << endl; cout << "Cube = " << cub << endl; return 0; } void opt(int n, int *pS, int *pC) { *pS = n*n; *pC = n*n*n; } Computer Programming and Basic Software Engineering 6. Pointers and Arrays sqr and cub of main() are changed by opt()

13 13 ?? 010001040108010c011001140118011c01200124 Address The stack Variables 3?? numsqr num, sqr and cub of main() 1. At main() Computer Programming and Basic Software Engineering 6. Pointers and Arrays cub pass-by-ref 01040108 pSpC n, pS and pC of opt() 2. When opt() is called 3 n pass-by-value *pS = n*n; *pC = n*n*n; 3. When *pS and *pC are computed in opt() 01040108 pSpC n, pS and pC of opt() 3 n 927 9 4. When opt() returns

14 14 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Passing an array of parameters to a function To save effort from separately passing a number of parameters to a function, they may be passed as an array. #include using namespace std; void opt(char * str); int main() { char string[] = {"This is a string"}; cout << string << endl; opt(string); cout << string << endl; return 0; } void opt(char * str) { strcpy(str,"New String"); } string[] is a character array, so string is a character pointer name of array is a pointer

15 15 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Note that in the previous example, string is modified inside the function opt() It is because passing the name of an array is pass-by- reference When copying the string "New String" to str in opt(), it is just the same as copying to the original string. Result of executing the last program string and str are the same

16 16 Exercise 6.3 The following program defines a class CAT that contains a private variable name[80]. A function is also defined. The function will swap the name of two cats by using pointers. Design the nameswap() function & the main() that will (i) create & initialize the name of two cats as Frisky & Felix in the stack. (ii) show the initial name of the two cats created (iii) swap the name of the two cats using the pointer approach (iv) show the name again. Computer Programming and Basic Software Engineering 6. Pointers and Arrays Only swap the name, not the object

17 17 #include using namespace std; class CAT {public: CAT(char * firstname) {strncpy(name,firstname,79);} ~CAT() {;} char * GetName() {return name;} void SetName(char *nameinput) {strncpy(name,nameinput,79);} private: char name[80]; }; void nameswap(CAT *CatA, CAT *CatB); Exercise 6.3 (Cont) Computer Programming and Basic Software Engineering 6. Pointers and Arrays


Download ppt "1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3."

Similar presentations


Ads by Google