CSC209H Worksheet: Function Pointers and System Call Error Checking
1. Remember that we can use the name of a function as the pointer to the function. This allows us to create variables that are pointers to functions. The syntax can be a little confusing.
For the statements below, identify whether the statement is A) a function signature, B) declaration of a function pointer variable, C) assigning the return value of a function to a variable, or D) assigning a pointer to a function to a variable.
Then label the relevant parts of the statement: variable name, return value, argument(s). Explain to your neighbour what each line means.
(a) int simple(char *str, int length);
(b) int (*x)(char *s, int l);
(c) int z;
z = simple(“abc”, 30)
(d) x = simple;
(e) int (*complex(int index))(char *s, int l);
(f) int (*y)(char *s, int z) = complex(2);
2. Add the error checking for the following calls. Discuss with your neighbour what the possible errors from the following system calls. (Feel free to cheat by reading the man page.) How important is it to check for errors? Should the program exit immediately?
FILE *fp;
fp = fopen(argv[1], “r”);
int num;
fread(&num, sizeof(int), 1, fp);
char *str;
str = malloc(sizeof(char) * 1024);