Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers as arguments and return values

Similar presentations


Presentation on theme: "Pointers as arguments and return values"— Presentation transcript:

1 Pointers as arguments and return values

2 Outline In this lesson, we will:
Pass pointers to local variables as parameters to functions Pass pointers to arrays as parameters to functions Pass pointer references as parameters to functions Return pointers from functions

3 Pointers to local variables as parameters
A function can take parameters that are of a pointer type void increment( int *p_x ) { *p_x = *p_x + 1; } int main() { int *p_int{new int{42}}; std::cout << "p_int: " << *p_int << std::endl; increment( p_int ); delete p_int; p_int = nullptr; return 0; Output: p_int: 42 p_int: 43

4 Pointers to local variables as parameters
A function can take multiple parameters that are of a pointer type void divide( double *p_y, int *p_x ) { if (*p_x != 0) { *p_y /= *p_x; } int main() { int *p_int{new int{42}}; double *p_db{new double{22.22}}; std::cout << "p_db: " << *p_db << std::endl; divide( p_db, p_int ); // deallocate p_int and p_db return 0; Output: p_db: 22.22 p_db:

5 Pointers to local variables as parameters
Pointers that are passed-by-value void swap( int *p_x, int *p_y ) { int *p_temp = p_x; p_x = p_y; p_y = p_temp; } int main() { int *p_int1{new int{42}}; int *p_int2{new int{33}}; std::cout << p_int1 << ", " << p_int2 << std::endl; swap( p_int1, p_int2 ); delete p_int1; p_int1 = nullptr; delete p_int2; p_int2 = nullptr; Output: 0x18c7c20, 0x18c7c40

6 Pointers to local variables as parameters
Pointers that are passed-by-reference void swap( int *&p_x, int *&p_y ) { int *p_temp = p_x; p_x = p_y; p_y = p_temp; } int main() { int *p_int1{new int{42}}; int *p_int2{new int{33}}; std::cout << p_int1 << ", " << p_int2 << std::endl; swap( p_int1, p_int2 ); delete p_int1; p_int1 = nullptr; delete p_int2; p_int2 = nullptr; Output: 0x19ebc20, 0x19ebc40 0x19ebc40, 0x19ebc20

7 Pointers as parameters
Recall that the address of the first element of the array is passed when passing an array as a parameter int sum( int data[], std::size_t capacity ) { int sum{0}; for ( std::size_t i{0}; i < capacity; ++i) { sum += data[i]; } return sum;

8 Pointers to arrays as parameters
A pointer can be used to pass the address of an element of an array int sum( int *a_data, std::size_t capacity ) { int sum{0}; for ( std::size_t i{0}; i < capacity; ++i ) { sum += a_data[i]; } return sum; int main() { std::size_t capacity{5}; int data[capacity]{1,2,3,4,5}; int *a_my_data{&data[0]}; std::cout << sum( a_my_data, capacity ) << std::endl; return 0; }

9 Pointers to arrays as parameters
A pointer can be used to pass the address of the starting element of a dynamically allocated array Must ensure to deallocate int sum( int *a_data, std::size_t capacity ) { int sum{0}; for (std::size_t i{0}; i < capacity; ++i) { sum += a_data[i]; } return sum; int main() { std::size_t capacity{5}; int *a_my_data{new int [capacity]{1, 2,3,4,5}}; std::cout << sum( a_my_data, capacity ) << std::endl; delete [] a_my_data; a_my_data = nullptr; return 0; }

10 Returning pointers A pointer can be returned from a function
int *create_dyn_array( int data[], std::size_t capacity) { int *a_darray{new (std::nothrow) int[capacity]}; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } return a_darray; A pointer to a dynamically created array is returned It must be deallocated later

11 Returning pointers A pointer can be returned from a function
int *create_dyn_array( int data[], std::size_t capacity) { int *a_darray{new (std::nothrow) int[capacity]}; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } return a_darray; A pointer to a dynamically created array is returned It must be deallocated later Good to check if it is nullptr int *a_array{create_dyn_array( data_array, capacity )}; if ( a_array == nullptr ) { // Handle this situation. }

12 Returning pointers Alternatively, you can catch exceptions of type std::bad_alloc that may be raised try { int * a_array{create_dyn_array( data_array, capacity )}; // Do something with the array } catch ( std::bad_alloc e ) { std::cout << “Error: memory not allocated.” << std::endl; // Handle this situation. }

13 Alternative to returning pointers
A pointer could also be passed as a parameter that is changed instead of returning a pointer void create_dyn_array( int data[], int *&a_darray, std::size_t capacity) { a_darray = new (std::nothrow) int[capacity]; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } A pointer to a dynamically created array can be accessed using a_darray

14 Quick review Write a function that deallocates a dynamically allocated array void deallocate_array (…) { // Write the code } Can you pass pointer pass-by-reference to change the contents of the memory pointed to by the pointer?

15 Summary Following this lesson, you now
Understand passing pointers as parameters to functions Pointers passed-by-value Pointers passed-by-reference Returning pointers from functions

16 References [1] No references?

17 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

18 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Pointers as arguments and return values"

Similar presentations


Ads by Google