0% found this document useful (0 votes)
1 views25 pages

04 2 PTR & Functions

The document discusses the use of pointers and reference parameters in C++, highlighting their differences and applications in functions. It includes examples such as swapping values, copying arrays, passing arrays to functions, and common errors related to pointers. The document emphasizes the importance of understanding pointers for effective data manipulation and memory management.

Uploaded by

機長小李
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views25 pages

04 2 PTR & Functions

The document discusses the use of pointers and reference parameters in C++, highlighting their differences and applications in functions. It includes examples such as swapping values, copying arrays, passing arrays to functions, and common errors related to pointers. The document emphasizes the importance of understanding pointers for effective data manipulation and memory management.

Uploaded by

機長小李
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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

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!
}

Parameter a – is a value parameter (it is a copy of z, the matching argument)


Parameter b – is a reference parameter (it is a reference to x, its matching argument)
Parameter c – is a reference parameter (it is a reference to y, its matching argument)
4
1. Pointers as parameters VS reference parameters

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( z, &x, &y );


cout << x << " " << y << " " << z;

mixUp return 0;
}

In the calling statement, pass the address of arguments: &x. &y

6
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;
*b = 60;
*c = 70;
}

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

mixUp( z, &x, &y );

void mixUp(int a, int *b, int *c);

8
x y z
main 20 30 40

a b c
mixUp 40 &x &y

mixUp( z, &x, &y );

void mixUp(int a, int *b, int *c);

9
x y z
main 20 30 40

a b c
mixUp 40 &x &y

mixUp( z, &x, &y );

void mixUp(int a, int *b, int *c);

10
x y z
main 20 30 40

a b c
mixUp 40 &x &y

void mixUp(int a, int *b, int *c)


{
a = 100;
*b = 60;
*c = 70;
}

11
x y z
main 20 60 30 70 40

a b c
mixUp 40 100 &x &y

void mixUp(int a, int *b, int *c)


{
a = 100;
*b = 60;
*c = 70;
}

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;

void swap(int a, int b) // . . .


{ swap(x, y);
int hold; // . . .
return 0;
hold = a; }
a = b;
b = hold;
}

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;

void swap(int *a, int *b) // . . .


{ swap(&x, &y);
int hold; // . . .
return 0;
hold = *a; }
*a = *b;
*b = hold;
}

15
Pointers as Parameters VS Reference Parameters
void swap(int *a, int *b); void swap(int &a, int &b);

int main(void) int main(void)


{ {
int x = 20; int x = 20;
int y = 30; int y = 30;

// . . . // . . .
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;

hold = *a; hold = a;


*a = *b; a = b;
*b = hold; b = 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)

int a[5] = { 98, 76, 84, 100, 95 };


int b[5];
int i;

for( i = 0; i < 5; i++ )


b[i] = a[i];

Writing b = a; does not work. Why?

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];.

These addresses may be used but not changed.

However, we do not wish to change &b[0], just to copy the contents


of a to b, and this must be done one element at a time.

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
}

void printAry ( double ary[], int arySize )


{
for( int i = 0; i < arySize; i++ )
cout << ary[i] << " ";
}

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
}

void printAry ( const double ary[], int arySize )


{
for( int i = 0; i < arySize; i++ )
cout << ary[i] << " ";
}

Q: Does this mean that arrays are always reference parameters?


A: Yes, and if you want to emphasize that a function should not change the array, declare the
array parameter constant, as shown above 21
5. Find the error
char *getFileName();

int main()
{
char *name;

name = getFileName();
cout << name << endl;
return 0;
}

char *getFileName()
{
char fileName[31];

cout << "File name: ";


cin >> fileName;
return fileName;
}
22
char *getFileName();

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)

cout << "File name: ";


cin >> fileName;
return fileName;
}
23
Pointers provide efficient techniques for
manipulating data in arrays.

Pointers are the basis for dynamic allocation


of memory.

Pointers could be used in functions for


reference parameters. ✔

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy