Chapter 15:
Pointers to Pointers and Array of Pointers
int myVar;
int *ptr;
myVar = 12;
ptr = &myVar;
int myVar;
int *ptr;
int **ptr_to_ptr;
myVar = 12;
ptr = &myVar;
ptr_to_ptr = &ptr;
int myVar;
int *ptr;
int **ptr_to_ptr;
myVar = 12;
ptr = &myVar;
ptr_to_ptr = &ptr;
“The two worlds” become “The three worlds”!
& is the “escalator up” * is the “escalator down”
ptr_to_ptr
ptr
myVar
Your turn:
Important application of p2p: Two-dimensional arrays
int a[3][4];
Important application of p2p: Two-dimensional arrays
int a[3][4];
What is common to all elements in a row?
Two-dimensional arrays
int a[3][4];
a[0]
a[1]
a[2]
Each row is an array!
Two-dimensional arrays
int a[3][4];
a[0]
a[1]
a[2]
This makes the matrix a an array of arrays
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
Example:
Source: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm
Source: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm
Output:
A pointer is a pointer is a pointer
Using sizeof with multi-dimensional arrays
EOL 0.5
QUIZ:
What is a 2D array in C?
QUIZ:
What is a 2D array in C?
A: An array of arrays!
QUIZ:
What is the name of the 2D array?
QUIZ:
What is the name of the 2D array?
A: A pointer to a pointer!
Pointer arithmetic with multi-dim arrays
The elements of multi are one-dimensional arrays of 4 integers
As with one-dim arrays, the name of a two-dim array is a constant
pointer
How can we create a copy of this pointer-to-pointer that we can
change?
Read from right to left (while starting inside the parenthesis!)
int (* p2p)[4];
p2p is a pointer to an array of 4 integers
Read from right to left (while starting inside the parenthesis!)
int (* p2p)[4];
p2p = multi;
p2p++;
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++;
Your turn:
Your turn:
Declare a pointer ppf to an array of 17 floats.
• What is the data type for num, ptr, and ptr2ptr?
• What is printed?
Source: http://www.c4learn.com/c-programming/c-double-pointer/
Can we do ptr2ptr = &&num ?
Source: http://www.c4learn.com/c-programming/c-double-pointer/
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.
Not in text: How to declare a 2D array w/malloc()
Zooming in on next slide
Here we’re only allocating memory for the first-level array!
When passing a 2D array to a function, we’re passing a p2p
Like 1D arrays, 2D arrays are passed by reference, not by value!
• 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!
3D array example
0 1 2
0
1
Using p2p to create a new data structure: jagged arrays
Reference: R. Reese, Understanding and Using C Pointers p.102, O’Reilly, 2013.
When are jagged arrays really useful?
Reference: R. Reese, Understanding and Using C Pointers p.102, O’Reilly, 2013.
Example: jagged array of characters
Passing a jagged array to a function
The main advantage of arrays of pointers is their flexibility!
Example: Efficient sorting by reordering of pointers! (Listing 15.7)
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