Lecture 6 FunctionPointers
Lecture 6 FunctionPointers
Department of Computing
Curtin University
1/20
Pointers to Functions Typedef Using Pointers to Functions
Outline
Pointers to Functions
Typedef
2/20
Pointers to Functions Typedef Using Pointers to Functions
Pointers to Functions
I Functions are stored in memory, just like variables.
I Pointers can point anywhere in memory, including to functions.
I There are special pointer types to represent this.
I These pointers can point to a function with specified
parameter/return types.
pointer
3/20
Pointers to Functions Typedef Using Pointers to Functions
4/20
Pointers to Functions Typedef Using Pointers to Functions
For example:
int (*ptr)(float x, int y);
5/20
Pointers to Functions Typedef Using Pointers to Functions
8/20
Pointers to Functions Typedef Using Pointers to Functions
Typedef
I The “typedef” keyword can be placed before any declaration.
I It converts the declaration into a “type declaration”.
I The name being declared instead becomes a new data type —
an alias.
I You can then use that name in place of the type it was
declared as.
I Normally used in header files.
Simplistic Example
typedef int INTEGER;
...
INTEGER num = 15;
10/20
Pointers to Functions Typedef Using Pointers to Functions
MagicData getMagic(void);
void doMagic(MagicData magic);
11/20
Pointers to Functions Typedef Using Pointers to Functions
14/20
Pointers to Functions Typedef Using Pointers to Functions
15/20
Pointers to Functions Typedef Using Pointers to Functions
Correct
Rule 4: Brackets override operator precedence.
int (*myPointer)(float,int);
16/20
Pointers to Functions Typedef Using Pointers to Functions
17/20
Pointers to Functions Typedef Using Pointers to Functions
18/20
Pointers to Functions Typedef Using Pointers to Functions
18/20
Pointers to Functions Typedef Using Pointers to Functions
18/20
Pointers to Functions Typedef Using Pointers to Functions
18/20
Pointers to Functions Typedef Using Pointers to Functions
19/20
Pointers to Functions Typedef Using Pointers to Functions
...
int (*myPointer)(float,int);
int result;
20/20