170 likes | 296 Views
Computer Programming and Basic Software Engineering. 6. Pointers and Arrays. Command-Line Processing. In many operating systems, command-line options are allowed to input parameters to the program. e.g. ipconfig /all. SomeProgram Param1 Param2 Param3.
E N D
Computer Programming and Basic Software Engineering 6. Pointers and Arrays Command-Line Processing • In many operating systems, command-line options are allowed to input parameters to the program e.g.ipconfig /all SomeProgram Param1 Param2 Param3 • To achieve this, main() allows two input parameters to hold the command-line options. You can also write *argv[] as**argv int main(int argc, char *argv[]) { return 0; } The values of argc and argv[]are provided indirectly argv is an array. Each element of the array is a character pointer argc stores the number of parameters
Computer Programming and Basic Software Engineering Command-Line Processing and Parameter Passing
Computer Programming and Basic Software Engineering 6. Pointers and Arrays Example SomeProgram Param1 Param2 Param3 main() is inside the projectSomeProgram int main(int argc, char *argv[]) { // function statements here return 0; } argc = 4 i.e. argv[0][0]is'S' argv[0] = "SomeProgram" argv[1] = "Param1" argv[2] = "Param2" argv[3] = "Param3"
Computer Programming and Basic Software Engineering Command-Line Processing 6. Pointers and Arrays The program can flow according to the command line options. #include <iostream> 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; } Every command line options will be printed out To add command-line options, this program must be run in command-line, not Start Without Debugging.
Computer Programming and Basic Software Engineering 6. Pointers and Arrays 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 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.
Computer Programming and Basic Software Engineering 6. Pointers and Arrays 6.3 Parameter passing using pointers
Computer Programming and Basic Software Engineering Pass Parameters – Pass by Value 6. Pointers and Arrays • We need to pass parameters to functions • We may pass parameters by value, i.e. pass copies of the parameter values to functions. #include <iostream> 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; } x andy are swapped only in swap() but not in main()
x and y of swap() x and y of swap() x x y y pass-by-value 10 5 10 5 temp = x; x = y; y = temp; 2. When swap() is called 3. When x and y is swapped Computer Programming and Basic Software Engineering 6. Pointers and Arrays x and y of main() x y Variables The stack 5 10 Address 0100 0104 0108 010c 0110 0114 0118 011c 0120 0124 4. When swap() returns 1. At main()
Computer Programming and Basic Software Engineering 6. Pointers and Arrays Pass by Reference #include <iostream> 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; } • pointerallows pass parameters by reference. The addresses of x and y in main() are passed to swap()
px and py of swap() px and py of swap() 10 5 px px py py 0100 0100 0104 0104 4. When swap() returns pass-by-ref 10 5 temp = *px; //5 *px = *py; //10 *py = temp; //5 2. When swap() is called 3. When *px and *py is swapped Computer Programming and Basic Software Engineering 6. Pointers and Arrays x and y of main() x y Variables The stack 5 10 Address 0100 0104 0108 010c 0110 0114 0118 011c 0120 0124 1. At main()
Computer Programming and Basic Software Engineering Return Multiple Values 6. Pointers and Arrays #include <iostream> 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; } • 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. sqr and cub of main() are changed by opt()
n, pS and pC of opt() n pS pC 9 9 27 3 27 0104 0104 0108 0108 pass-by-ref pass-by-value *pS = n*n; *pC = n*n*n; 4. When opt() returns 2. When opt() is called 3. When *pS and *pC are computed in opt() n, pS and pC of opt() n pS pC 3 Computer Programming and Basic Software Engineering 6. Pointers and Arrays num, sqr and cub of main() num sqr cub Variables The stack 3 ?? ?? Address 0100 0104 0108 010c 0110 0114 0118 011c 0120 0124 1. At main()
Computer Programming and Basic Software Engineering 6. Pointers and Arrays Passing an array of parameters to a function name of array is a pointer #include <iostream> #include <string.h> 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"); } • To save effort from separately passing a number of parameters to a function, they may be passed as an array. string[] is a character array, so string is a character pointer
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. stringand strare the same Result of executing the last program
Computer Programming and Basic Software Engineering 6. Pointers and Arrays 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 • create & initialize the name of two cats as Frisky & Felix in the stack. • show the initial name of the two cats created • swap the name of the two cats using the pointer approach • show the name again. Only swap the name, not the object
Computer Programming and Basic Software Engineering 6. Pointers and Arrays Exercise 6.3 (Cont) #include <iostream> #include <string> 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);