04 2 PTR & Functions
04 2 PTR & Functions
Examples:
1. Pointers as parameters VS reference parameters
2. Swap – fix errors
3. Copy an array to another array
4. Pass an array to a function
5. Find the error – return a pointer from a function
1
1. Pointers as parameters VS reference parameters
main
mixUp
2
int main()
{
int x = 20;
main int y = 30;
int z = 40;
mixUp( z, x, y );
cout << x << " " << y << " " << z;
mixUp return 0;
}
3
int main()
{
int x = 20;
main int y = 30;
int z = 40;
mixUp( z, x, y );
cout << x << " " << y << " " << z;
return 0;
mixUp }
/* =========================== */
void mixUp(int a, int &b, int &c)
{
a = 100; // z in main() does not change
b = 60; // x in main() does change!
c = 70; // y in main() does change!
}
main
mixUp
In C++ reference parameters are based on pointers; we can use pointers instead of
reference parameter!
5
int main()
{
int x = 20;
main int y = 30;
int z = 40;
mixUp return 0;
}
6
int main()
{
int x = 20;
main int y = 30;
int z = 40;
In the function definition, declare the matching parameters to be pointers, and dereference
them when used in statements.
7
x y z
main 20 30 40
a b c
mixUp
8
x y z
main 20 30 40
a b c
mixUp 40 &x &y
9
x y z
main 20 30 40
a b c
mixUp 40 &x &y
10
x y z
main 20 30 40
a b c
mixUp 40 &x &y
11
x y z
main 20 60 30 70 40
a b c
mixUp 40 100 &x &y
12
x y z
main 60 70 40
mixUp Output: 60 70 40
13
2. Swap – fix errors
main
void swap(int a, int b);
int main(void)
{
int x = 20;
swap int y = 30;
14
swap( x, y); // this call does nothing to x and y
swap(&x, &y); // use pointers
main
void swap(int *a, int *b);
int main(void)
{
int x = 20;
swap int y = 30;
15
Pointers as Parameters VS Reference Parameters
void swap(int *a, int *b); void swap(int &a, int &b);
// . . . // . . .
swap(&x, &y); swap(x, y);
// . . . // . . .
return 0; return 0;
} }
/* ===================== */ /* ===================== */
void swap(int *a, int *b) void swap(int &a, int &b)
{ {
int hold; int hold;
In summary, reference parameters are easier to work with, therefore we will continue to use them.
Knowing pointers gives us a better understanding of reference parameters.
16
3. Make a copy of an array
– one element at a time (assume the two arrays have the same size, 5)
17
The name of an array represents the address of its first element. In
other words b = a; is equivalent to &b[0] = &a[0];.
18
4. Pass an array to a function
int main()
{
int size = 5;
double a[9] = { 2.1, 3.5, 1.7, 8.9, 5.6 };
. . .
printAry( a, size );
return 0; &a[0] 5
}
The name of an array in C++ represents the address of its first element (it is a pointer constant)
a &a[0]
19
int main()
{
int size = 5;
double a[9] = { 2.1, 3.5, 1.7, 8.9, 5.6 };
. . .
printAry( a, size );
return 0;
}
&a[0] 5
void printAry ( double ary[], int arySize )
{
for( int i = 0; i < arySize; i++ )
cout << ary[i] << " ";
}
a &a[0]
20
4. Pass an array to a function
int main()
{
int size = 5;
double a[9] = { 2.1, 3.5, 1.7, 8.9, 5.6 };
. . .
printAry( a, size );
return 0; &a[0] 5
}
int main()
{
char *name;
name = getFileName();
cout << name << endl;
return 0;
}
char *getFileName()
{
char fileName[31];
int main()
{ Do not return the address of
char *name;
a local variable!
name = getFileName();
cout << name << endl; Although it is possible to return a pointer
return 0; from a function (we’ll see such examples
later), never return the address of a local
} variable, since local variables no longer
exist after the call).
char *getFileName()
{ Note: the name of an array in C++
char fileName[31]; represents the address of its first element
(it is a pointer constant)
24
Pointers and Functions
Examples:
ü 1. Pointers as parameters VS reference parameters
ü 2. Swap – fix errors
ü 3. Copy an array to another array
ü 4. Pass an array to a function
ü 5. Find the error – return a pointer from a function
25