Pointer declaration and arithmetic, malloc, calloc, realloc, free, function pointers, memory leaks and dangling pointers.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- How do you declare a pointer to an integer in C?
Show answer
int *ptr; or int* ptr;
- What does the & (address-of) operator return?
Show answer
The memory address of a variable
- What does the * (dereference) operator do?
Show answer
Accesses the value stored at the address the pointer points to
- What is the purpose of a void pointer?
Show answer
A generic pointer that can point to any data type
- If ptr points to an int and you execute ptr++, how many bytes does the address increase by?
Show answer
The size of an int, typically 4 bytes on most systems
- What is the result of subtracting one pointer from another?
Show answer
The number of elements between them in memory
- What is the difference between ptr++ and ++ptr when advancing a pointer in a loop?
Show answer
Post-increment returns the old value, pre-increment returns the new value, but both advance the pointer the same amount
- What value does a NULL pointer represent?
Show answer
An uninitialized or invalid address, typically defined as 0 or 0x0
- What does malloc stand for?
Show answer
Memory allocation
- What does malloc return if it fails to allocate memory?
Show answer
NULL (a null pointer)
- Which header file contains malloc?
Show answer
stdlib.h
- Does malloc initialize allocated memory to any specific value?
Show answer
No, the memory contains garbage values
- What is the correct syntax to allocate an array of 10 integers using malloc?
Show answer
int *arr = (int*)malloc(10 * sizeof(int));
- What are the two parameters of the calloc function?
Show answer
The number of elements and the size of each element in bytes
- What is the main advantage of calloc over malloc?
Show answer
calloc initializes all allocated memory to zero