CS_8251 Function Pointers
Function pointers are a bit difficult to grok. Use these flashcards to know more about them. Thanks to https://cdecl.org/
13 flashcards · Programming Languages · by kgashok
int bar(); bar is declared as a function returning an int. True or False?
True
int bar(); int (*foo)(); Assertion 1: bar is declared as a function returning an int. Assertion 2: foo is declared as a pointer to function returning int Is Assertion_2 true or false?
True
if int bar(); int (*foo)(); then foo = bar; A. A compile error will occur B. A compile error will never occur C. It will compile provided bar function is defined elsewhere
Choice C. Also, int (*foo)(); // declaration foo = bar; // initialisation can also be shortened to one line as int (*foo)() = bar(); // declare and init
int (*func_ptr)(int); What is the above code doing?
It declares func_ptr as pointer to function (int) returning int. func_ptr points to a function that takes one argument (of type int) and returns a value that is of type int.
If int (*func_ptr)(int); and int add_2 (int n) { return n + 2; } Will the below code snippet compile or not? func_ptr = add_2; printf("%d\n", func_ptr(4));
It will compile. And the output will be 6. (4 + 2)
int (*convcase[2])(int); What is the above code doing?
It declares convcase as array 2 of pointer to function (int) returning int
If int (*convcase[2])(int) = {toupper, tolower}; then convcase[0]('a') will produce what value? convcase[1]('B') will produce what value? convcase[1]('z') will produce what value?
A b z
If int togglecase(int c) { return isalpha(c) ? c ^ 32 : c; } int (*convcase[3])(int) = {toupper, tolower, togglecase}; then convcase[3]('Z') will produce what value? convcase[3]('z') will produce what value?
z Z
declare comparators as array 2 of pointer to function (pointer to const void, pointer to const void) returning int
int (*comparators[2])(const void*, const void*);
int (*(*foo)(void ))[3]; What the above code is doing?
declare foo as pointer to function (void) returning pointer to array 3 of int
int **p; What is the above code doing?
declare p as pointer to pointer to int or p as double pointer to int
Write the code to declare cat_functions as an array of size 2 of pointer to function (pointer to char, pointer to const char) returning pointer to char
char *(*cat_functions[2])(char *, const char *)
Assume source and dest are valid NTBSs (null defined byte string). Define and initialise a suitable pointer to function variable so that str_function[1](source, dest) invokes strcpy str_function[2](source, dest) invokes strcat where strcpy and strcat are standard string library functions of C.
char *(*str_functions[3])(char *, const char*) = {NULL, strcpy, strcat};