Pointer short qs Flashcards

(7 cards)

1
Q

How are pointers (not function pointers) used in ‘C’?

A

Pointers in C are primarily used to store the memory address of another variable. They allow for indirect access and manipulation of data.

Key uses: Dynamic memory allocation, Array manipulation

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

What are the steps involved in using pointers? Illustrate your answer with code snippets.

A
  1. Declaration
    Declare the pointer by specifing the type of data it points to, followed by an asterisk(*)
    eg. int *ptr;
  2. Initialization (Assigning an address)
    Initialize the pointer by assigning it the memory address of an existing variable using the address-of operatior (&)
int a = 2;
ptr = &a;
  1. Dereferencing (Accessing Value)
    Access the value stored at the memory address the pointer holds using the dereference operator (*)
int value = *ptr;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can the type void* be used for? Give a simple example, including
assigning a value to a variable of type void*.

A

void * is used as a generic pointer that can hold the memory address of any data type.

int a = 67;
void *ptr; // void pointer

ptr = &a;  // assign address of an int to void*

int y = *(int *)ptr; // cast void* to int* before dereferencing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe how function pointers can be used in ‘C’.In what ways do function pointers differ from other pointers? Illustrate your answer with code snippets.

A

Function pointers allow functions to be passed as arguments to other functions, enabling generic functions whose behaviour depends on the function supplied.
Structure:

return type (*pointer_name) (parameters)
int add(int a, int b){
   return a + b;
}

int main(){
    int (*ptr_add)(int,int)
    ptr_add = add;

  int result = (*ptr_add)(5,4);

  return 0;

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

In what situations would function pointers be useful (and why)?

A

Callbacks: Passing a functions address to another function so it can be excecuted upon an event like a button click

Generic algorithms: Allowing a standard function (like qsort) to perform a custom action (like comparison logic) supplied via the pointer

Jump tables: Storing pointers in an array to quickly execute a chosen function based on an index, rather than a large switch statment.

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

What are the advantages of using function pointers
in ‘C’. Illustrate your answer with code snippets.

A

Advantages:
1. Flexibility: Allows the same code to work with different functions

int apply(int (*funct)(int,int), int a, int b){
   return funct(a,b);
}
  1. Runtime behaviour selection: Function chosen at runtime(Callbacks)
void onEvent(void (*handler)()){
   handler();
}
  1. Reduced code duplication: avoids writing similar functions for different behaviour.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the disadvantages of using function pointers
in ‘C’. Illustrate your answer with code snippets.

A
  1. Harder to read and maintain: Syntax is less intuative than normal function calls
int (*fp)(int,int);
  1. No compile-time behaviour checking: Wrong function signatures can cause runtime errors.
  2. Slight performance overhead: Indirect calls can be slower than direct calls
How well did you know this?
1
Not at all
2
3
4
5
Perfectly