Multi-Dimensional Arrays
Taking Multi-Dimensional Arrays Apart
Comparison Pointers
Type Matters
When using palette and I just say palette, that’s a reference to the first row and it moves rows when I do pointer arihtmetic. When passing it to a function: I can do void f( short pal[][3] )
{
. . .;
} which will evaluate pal to pointer
What if I want to dynamically
allocate this array?
We’ll need to be able to declare a pointer with this type.
Taking Multi-Dimensional Arrays Apart(Pointer Syntax for 2D Array
Alternative Array Syntax for 2D Arrays and Beyond
MultiDimensional Arrays (dynamically allocate 2D array)
MultiDeimension Arrays(Dynamically Allocate 2D Array Full Example)
Freeing Memory
Arrays of Pointers
Array of Strings
Pointers Functions
Storing Function Pointers
Using Function Pointers
int stringLength( char str )
{
char p = str;
while ( *p )
p++;
return p - str;
}
int main( int argc, char *argv[] )
{
int (f)( char * );
f = stringLength;
for ( int i = 0; i < argc; i++ ) {
int result = f( argv[ i ] );
printf( “%d\n”, result );
}
…
Print the length of each string on the
command line.
This is worth something, but it’s not how you
normally use function pointers
int applyFunction( char **strings,
int stringCount,
int (f)( char * ) )
{
int total = 0;
for ( int i = 0; i < stringCount; i++ ) {
total += f( strings[ i ] );
}
return total;
}
This is more typical.
This will apply any function f to the list of strings, returning the total.
applyFunction( argv, argc, stringLength );
applyFunction( argv, argc, countVowels );
We can get applyFunction() to use
different functions, without even knowing
the particular function it’s using.
Reading Types
Fun With Reading Types
Function Pointers in the Standard
Library
Sort Examples
Sorting Char Pointers
Thinking About Function Pointers