44
Chapter 15: Pointers to Pointers and Array of Pointers

Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Chapter 15:

Pointers to Pointers and Array of Pointers

Page 2: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

int myVar;

int *ptr;

myVar = 12;

ptr = &myVar;

Page 3: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

int myVar;

int *ptr;

int **ptr_to_ptr;

myVar = 12;

ptr = &myVar;

ptr_to_ptr = &ptr;

Page 4: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

int myVar;

int *ptr;

int **ptr_to_ptr;

myVar = 12;

ptr = &myVar;

ptr_to_ptr = &ptr;

Page 5: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

“The two worlds” become “The three worlds”!

& is the “escalator up” * is the “escalator down”

ptr_to_ptr

ptr

myVar

Page 6: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Your turn:

Page 7: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Important application of p2p: Two-dimensional arrays

int a[3][4];

Page 8: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Important application of p2p: Two-dimensional arrays

int a[3][4];

What is common to all elements in a row?

Page 9: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Two-dimensional arrays

int a[3][4];

a[0]

a[1]

a[2]

Each row is an array!

Page 10: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Two-dimensional arrays

int a[3][4];

a[0]

a[1]

a[2]

This makes the matrix a an array of arrays

Page 11: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

a

a[0]

a[0][1]

a[1] a[2]

a[1][3] a[2][0]

a[2][3] a[2][1]

ptr_to_ptr

ptr

myVar

Page 12: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Example:

Source: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

Page 13: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Source: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

Output:

Page 14: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

A pointer is a pointer is a pointer

Page 15: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Using sizeof with multi-dimensional arrays

EOL 0.5

Page 16: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

QUIZ:

What is a 2D array in C?

Page 17: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

QUIZ:

What is a 2D array in C?

A: An array of arrays!

Page 18: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

QUIZ:

What is the name of the 2D array?

Page 19: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

QUIZ:

What is the name of the 2D array?

A: A pointer to a pointer!

Page 20: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Pointer arithmetic with multi-dim arrays

The elements of multi are one-dimensional arrays of 4 integers

Page 21: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

As with one-dim arrays, the name of a two-dim array is a constant

pointer

Page 22: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

How can we create a copy of this pointer-to-pointer that we can

change?

Page 23: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Read from right to left (while starting inside the parenthesis!)

int (* p2p)[4];

p2p is a pointer to an array of 4 integers

Page 24: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Read from right to left (while starting inside the parenthesis!)

int (* p2p)[4];

p2p = multi;

p2p++;

Page 25: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

If the array multi starts at address 1000 (decimal), what is p2p at the

end of the code below?

int (* p2p)[4];

p2p = multi;

p2p++;

Page 26: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Your turn:

Page 27: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Your turn:

Declare a pointer ppf to an array of 17 floats.

Page 28: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

• What is the data type for num, ptr, and ptr2ptr?

• What is printed?

Source: http://www.c4learn.com/c-programming/c-double-pointer/

Page 29: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Can we do ptr2ptr = &&num ?

Source: http://www.c4learn.com/c-programming/c-double-pointer/

Page 30: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Your turn:

• Declare an array a of 3x5 characters.

• Copy the array name into a user-defined pointer-to-pointer b.

• Use b and pointer arithmetic to display the last element on the second row of a.

Page 31: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Not in text: How to declare a 2D array w/malloc()

Zooming in on next slide

Page 32: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Here we’re only allocating memory for the first-level array!

Page 33: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

When passing a 2D array to a function, we’re passing a p2p

Page 34: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Like 1D arrays, 2D arrays are passed by reference, not by value!

Page 35: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

• Listing 15.4 in our text gives two different examples of passing 2D arrays to functions, but they’re less general:

• The number of columns (4) is hard-coded in the functions’ headers.

• Read and understand Listing 15.4, but use the method in the previous slide for reference!

Page 36: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

3D array example

0 1 2

0

1

Page 37: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Using p2p to create a new data structure: jagged arrays

Reference: R. Reese, Understanding and Using C Pointers p.102, O’Reilly, 2013.

Page 38: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

When are jagged arrays really useful?

Reference: R. Reese, Understanding and Using C Pointers p.102, O’Reilly, 2013.

Page 39: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To
Page 40: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Example: jagged array of characters

Page 41: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

Passing a jagged array to a function

Page 42: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

The main advantage of arrays of pointers is their flexibility!

Example: Efficient sorting by reordering of pointers! (Listing 15.7)

Page 43: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To
Page 44: Pointers to Pointers and Array of Pointers · The main advantage of arrays of pointers is their flexibility! Example: Efficient sorting by reordering of pointers! (Listing 15.7) To

To do for next time:

• Read and take notes: Up to Listing 15.7

• Exercises 1, 2, 3

• Enter in the IDE the program from Listing 15.7 and test it by entering only three strings of your choice