Pointers Flashcards
(7 cards)
What is a pointer in C? How do you declare an integer pointer ptr?
A variable that stores the memory address of another variable. Declared as int *ptr;.
What are the & and * operators used for with pointers?
& (Address-of Operator): Returns the memory address of a variable.
- (Dereference Operator): Accesses the value at the memory address stored in a pointer.
How can you access array elements using pointer arithmetic?
array[i] is equivalent to *(array + i)
What is dynamic memory allocation, and what are the four main functions for it in <stdlib.h>?</stdlib.h>
Allocating memory during program execution (runtime). Functions: malloc(), calloc(), realloc(), free().
What is the key difference between malloc() and calloc()?
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.
Why is it crucial to use free() after dynamic memory allocation?
To deallocate the memory and prevent memory leaks, making it available for reuse.
List two drawbacks of using pointers.
Complexity (hard to use/debug), uninitialized pointers (segmentation faults), memory leaks (if not free()d), memory corruption.