Important Introduction to Programming Short Questions
Chapter: 14
Question 1:
What does the *const pointer declaration mean?
Answer:
The declaration int *const ptr; means that ptr is a constant pointer to an integer, meaning the pointer itself cannot point to another memory address after initialization.
Chapter: 14
Question 2:
How do you swap two values using pointers?
Answer:
To swap two values using pointers, you pass their addresses to a function that interchanges the values by dereferencing the pointers and using a temporary variable for the swap.
Chapter: 14
Question 3:
What happens when you pass an array to a function in C?
Answer:
When an array is passed to a function in C, the starting address of the array is passed, allowing the function to manipulate the array elements directly.
Chapter: 14
Question 4:
What is the difference between int *const ptr and const int *ptr?
Answer:
int *const ptr is a constant pointer to an integer, meaning the pointer cannot change its target, while const int *ptr is a pointer to a constant integer, meaning the value at the address cannot be modified.
Chapter: 14
Question 5:
How do you declare multiple pointers of the same type?
Answer:
Multiple pointers of the same type can be declared using a comma-separated list, ensuring each pointer is preceded by an asterisk (*), e.g., int *ptr1, *ptr2, *ptr3;.
Chapter: 14
Question 6:
What does swap(int *x, int *y) function do?
Answer:
The swap(int *x, int *y) function interchanges the values of the two variables whose addresses are passed, using the pointers to access and modify the values.
Chapter: 14
Question 7:
How is a pointer initialized with a variable's address?
Answer:
A pointer is initialized with a variable's address by using the address-of operator &, for example, ptr = &x; where ptr will now point to x.
Chapter: 14
Question 8:
Why is it a good practice to initialize pointers?
Answer:
Initializing pointers is good practice because it prevents the pointer from holding a garbage address, which could lead to undefined behavior when dereferenced.
Chapter: 14
Question 9:
What does *ptr mean in a program?
Answer:
*ptr means accessing the value stored at the memory address pointed to by the pointer ptr.
Chapter: 14
Question 10:
How does call by reference help in efficiency?
Answer:
Call by reference helps in efficiency by avoiding the need to copy large amounts of data; instead, only the address is passed, allowing the function to work directly with the original data.