1. What is a pointer in C?
Answer:
Explanation:
In C, a pointer is a variable that stores the memory address of another variable.
2. How do you declare a pointer in C?
Answer:
Explanation:
A pointer is declared by specifying the data type of the variable it points to, followed by an asterisk (*) and the pointer name.
3. Which of the following operators is used to access the value pointed to by a pointer?
Answer:
Explanation:
The dereference operator (*) is used to access the value at the memory address stored in a pointer.
4. What does the '&' operator do in C?
Answer:
Explanation:
The address-of operator (&) is used to obtain the memory address of a variable.
5. What is the output of the following C code?
Answer:
Explanation:
The pointer ptr is assigned the address of var. Using the dereference operator (*ptr), the value at that address, which is 5, is accessed and printed.
6. How can you increment the value pointed to by a pointer in C?
Answer:
Explanation:
Both (*ptr)++ and ++*ptr increment the value pointed to by ptr. The parentheses ensure that the increment operation is applied to the value pointed to by ptr, not to the address stored in ptr.
7. What is the correct way to declare a pointer to a pointer in C?
Answer:
Explanation:
A pointer to a pointer is declared by using two asterisks (**). It means that 'ptr' is a pointer to another pointer.
8. What is a NULL pointer in C?
Answer:
Explanation:
A NULL pointer in C is a pointer that does not point to any valid memory location. It is used to indicate that the pointer is not in use.
9. How do you dynamically allocate memory in C?
Answer:
Explanation:
Memory in C can be dynamically allocated using the malloc() function from the stdlib.h library.
10. What is pointer arithmetic in C?
Answer:
Explanation:
Pointer arithmetic involves adding or subtracting integers to or from pointers, or comparing pointers.
11. What is the result of comparing two pointers in C?
Answer:
Explanation:
Comparing two pointers results in a boolean value indicating whether they point to the same memory location.
12. What is the output of the following C code?
Answer:
Explanation:
The pointer ptr initially points to the first element of arr. The expression *++ptr increments the pointer, making it point to the second element, and then dereferences it, printing 20.
13. Which of the following is a correct way to pass a pointer to a function in C?
Answer:
Explanation:
A pointer can be passed to a function either by passing the pointer itself or by passing the address of the pointer.
14. Can a function return a pointer in C?
Answer:
Explanation:
A function in C can return a pointer. However, care must be taken to ensure that the pointer does not point to a variable that goes out of scope once the function returns.
15. What is the purpose of the const keyword with pointers in C?
Answer:
Explanation:
The const keyword can be used with pointers to either make the pointer itself constant, the value it points to constant, or both.
16. How do you declare an array of pointers in C?
Answer:
Explanation:
An array of pointers in C is declared by placing an asterisk (*) before the array name, indicating that each element of the array is a pointer.
17. How do you access a member of a structure using a pointer to that structure in C?
Answer:
Explanation:
You can access a member of a structure using a pointer either with the arrow operator (->) or by dereferencing the pointer and using the dot operator (.)
18. What happens if you try to dereference a NULL pointer in C?
Answer:
Explanation:
Dereferencing a NULL pointer is dangerous as it leads to undefined behavior, often causing a runtime error like a segmentation fault.
19. How do you free dynamically allocated memory in C?
Answer:
Explanation:
Dynamically allocated memory in C must be manually freed using the free() function.
20. What is the output of the following C code?
Answer:
Explanation:
The sizeof operator when used with a pointer variable returns the size of the pointer itself, not the size of the data it points to.
Comments
Post a Comment
Leave Comment