C 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 a pointer variable by specifying the type of data it points to, followed by an asterisk (*).
    int *ptr;
  2. Initialization (Assigning an Address)
    Initialize the pointer by assigning it the memory address of an existing variable using the address-of operator (&).
    int x = 10;
    ptr = &x;
  3. Dereferencing (Accessing Value)
    Access the value stored at the memory address the pointer holds using the dereference operator or indirection operator (*).
    int value = *ptr;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is dynamic memory allocation handled in ‘C’? In your own words

A

During runtime
- The memory is allocated from the heap
- Allows a program to manage a variable amount of memory rather than relying on fixed memory allocated at compile time.

The standard C library provides 4 primary functions to manage memory dynamically:

malloc: Allocates a block of uninitialized memory of a specified size and returns a void * pointer to the start of the block

calloc: Allocates an array in memory for a specified number of elements with all bits initialized to 0, also returns a void * pointer

realloc: Changes the size of a previously allocated block of memory, takes the old pointer and the new size.

free: Releases the dynamically allocated memory back to the system’s heap. Crucial for preventing memory leaks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
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 main(){
int num = 32;
void *genPointer = #

printf(“The void pointer must be cast to the correct type: %d”, * (int *)genPointer);
}

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

Describe how function pointers can be used in ‘C’.

A

Variable that stores the address of an executable function.

return_type (*pointer_name)(parameter_list);

int add(int a, int b){
return a + b;
}

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

 ptr_add = add;

int result = (*ptr_add)(5,3);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In what ways do function pointers differ
from other pointers? Illustrate your answer with code snippets.

A
  1. Dereferencing
    - Dereferencing *ptr retrieves the data value stored at the address
    - Dereferencing fps retrieves the function code at the address, allowing it to be executed
  2. Pointer arithmetic
    - You can increment/decrement a data pointer (ptr++)
    - Pointer arithmetic is not allowed on function pointers as it makes no functional sense.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

Callbacks
Passing a functions address to another function so it can be executed 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 statement

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