Title: Wavelet transform And Its Applications to Image Processing
1Computer 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 argvare provided
indirectly
argv is an array. Each element of the array is a
character pointer
argc stores the number of parameters
2Computer Programming and Basic Software
Engineering
Command-Line Processing and Parameter Passing
3Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Example
SomeProgram Param1 Param2 Param3
main() is inside the project SomeProgram
int main(int argc, char argv) // function
statements here return 0
argc 4
i.e. argv00 is 'S'
argv0 "SomeProgram" argv1
"Param1" argv2 "Param2" argv3 "Param3"
4Computer Programming and Basic Software
Engineering
Command-Line Processing
6. Pointers and Arrays
The program can flow according to the command
line options.
include ltiostreamgt using namespace std int
main(int argc, char argv) cout ltlt
"Received " ltlt argc ltlt " arguments." ltlt endl
for (int i 0 iltargc i) cout ltlt
"argument " ltlt i ltlt " " ltlt argvi ltlt 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.
5Computer 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.
6Computer 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.
7Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
6.3 Parameter passing using pointers
8Computer 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 ltiostreamgt using namespace std void
swap(int x, int y) int temp temp
x xy ytemp int main() int x 5, y
10 cout ltlt "Main. Before swap, x " ltlt x ltlt
"y " ltlt y ltlt "\n" swap(x,y) cout ltlt "Main.
After swap, x " ltlt x ltlt " y " ltlt y ltlt
"\n" return 0
x and y are swapped only in swap() but not in
main()
9Computer 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()
4. When swap() returns
10Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Pass by Reference
include ltiostreamgt using namespace std void
swap(int px, int py) int temp temp
px pxpy pytemp int main() int x
5, y 10 cout ltlt "Main. Before swap, x "
ltlt x ltlt " y " ltlt y ltlt "\n" swap(x,y) cout
ltlt "Main. After swap, x " ltlt x ltlt " y " ltlt y
ltlt "\n" return 0
- pointer allows pass parameters by reference.
The addresses of x and y in main() are passed to
swap()
11Computer 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()
12Computer Programming and Basic Software
Engineering
Return Multiple Values
6. Pointers and Arrays
include ltiostreamgt using namespace std void
opt(int, int , int ) //prototype int
main() int num, sqr, cub cout ltlt "Input a
number " cin gtgt num opt(num, sqr,
cub) cout ltlt "Square " ltlt sqr ltlt
endl cout ltlt "Cube " ltlt cub ltlt endl return
0 void opt(int n, int pS, int pC) pS
nn pC nnn
- 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()
13Computer 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()
14Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Passing an array of parameters to a function
name of array is a pointer
include ltiostreamgt include ltstring.hgt using
namespace std void opt(char str) int main()
char string "This is a string" cout ltlt
string ltlt endl opt(string) cout ltlt string
ltlt 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
15Computer 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.
string and str are the same
Result of executing the last program
16Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Exercise 6.3
- The following program defines a class CAT that
contains a private variable name80. 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
17Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Exercise 6.3 (Cont)
include ltiostreamgt include ltstringgt 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,7
9) private char name80 void
nameswap(CAT CatA, CAT CatB)