In C, a pointer is a variable that stores the memory address of another variable. Using pointers, you can directly interact with memory locations, enabling various operations like dynamic memory allocation, array iterations, and function arguments passed by reference.
Let’s dive into the world of pointers in C and assess your comprehension with some multiple choice questions! Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What does a pointer store?
Answer:
Explanation:
A pointer stores the memory address of another variable.
2. Which operator is used to get the address of a variable?
Answer:
Explanation:
The & operator is used to fetch the address of a variable.
3. Which operator is used to declare a pointer variable?
Answer:
Explanation:
The * operator is used in the declaration of a pointer variable.
4. What does the following declaration mean?
int *ptr;
Answer:
Explanation:
This declaration means that ptr is a pointer variable that can store the address of an int data type.
5. What is the output of the following code?
int a = 10;
int *p;
p = &a;
printf("%d", *p);
Answer:
Explanation:
The pointer p is assigned the address of a and *p dereferences the pointer, giving the value of a which is 10.
6. Which keyword is used to allocate dynamic memory in C?
Answer:
Explanation:
malloc stands for memory allocation and is used for dynamic memory allocation.
7. What will be the output of the following code segment?
int a = 5, *p = &a;
printf("%d", *p+2);
Answer:
Explanation:
The value at pointer p is 5 (which is the value of a). So, *p + 2 = 5 + 2 = 7.
8. Which function is used to release dynamically allocated memory?
Answer:
Explanation:
The free function is used to release memory that was previously allocated by malloc.
9. What is a NULL pointer?
Answer:
Explanation:
A NULL pointer is a pointer that does not point to any memory location.
10. What is a dangling pointer?
Answer:
Explanation:
A dangling pointer is a pointer that points to a memory location that has been deleted or deallocated.
Understanding pointers is crucial in C programming as they provide a way to utilize the capabilities of the memory efficiently. We hope this quiz has enhanced your knowledge. Keep practicing and exploring more about pointers!