Pointers Flashcards

(7 cards)

1
Q

What is a pointer in C? How do you declare an integer pointer ptr?

A

A variable that stores the memory address of another variable. Declared as int *ptr;.

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

What are the & and * operators used for with pointers?

A

& (Address-of Operator): Returns the memory address of a variable.

  • (Dereference Operator): Accesses the value at the memory address stored in a pointer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you access array elements using pointer arithmetic?

A

array[i] is equivalent to *(array + i)

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

What is dynamic memory allocation, and what are the four main functions for it in <stdlib.h>?</stdlib.h>

A

Allocating memory during program execution (runtime). Functions: malloc(), calloc(), realloc(), free().

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

What is the key difference between malloc() and calloc()?

A

malloc(): Allocates a block of memory, but the allocated memory contains garbage values.

calloc(): Allocates memory for multiple elements and initializes all allocated bytes to zero.

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

Why is it crucial to use free() after dynamic memory allocation?

A

To deallocate the memory and prevent memory leaks, making it available for reuse.

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

List two drawbacks of using pointers.

A

Complexity (hard to use/debug), uninitialized pointers (segmentation faults), memory leaks (if not free()d), memory corruption.

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