Functions, Pointers, Structures Flashcards

1
Q

What is a function pointer in C?

A

A variable that stores the address of a function, allowing functions to be passed around and called indirectly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare a function pointer for a function that takes two int arguments and returns an int?

A

int (*f)(int, int);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you assign a function to a function pointer?

A

f = ∑ or f = sum; (both are valid)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you call a function using a function pointer?

A

f(a, b); or (*f)(a, b);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some uses of function pointers in C?

A

Passing functions as arguments

Implementing callbacks

Building lookup tables for function dispatch

Writing modular code (e.g., sorting with custom comparators)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are function pointers used with qsort() in C?

A

You pass a comparator function that matches the signature expected by qsort, enabling custom sorting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can function pointers be members of a struct?

A

Yes, they can be used to define behavior in data structures, similar to methods in object-oriented programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide an example of a function pointer inside a struct.

A

typedef struct {
int (*operation)(int, int);
} Operator;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In the example operate(sum, a, b), what is sum?

A

A function passed as an argument to operate, which will call it using a function pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does this function pointer declaration mean: int (*f)(int, int);?

A

f is a pointer to a function that takes two int arguments and returns an int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a function pointer table and how is it useful?

A

An array of function pointers used to select and call functions dynamically, often used in interpreters or state machines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can function pointers help emulate object-oriented behavior in C?

A

By combining data (structs) with function pointers (methods), we can mimic encapsulation and polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What should you always ensure when using function pointers?

A

The function pointer must be initialized before use, and its signature must match the target function exactly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a common pitfall with function pointers?

A

Mismatching function signatures or dereferencing an uninitialized/null pointer can cause undefined behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly