In the expression: int arr[5]; int *p = arr; What does p + 2 represent?
Pointer arithmetic scales by data type size. p + 2 moves 2 * sizeof(int) bytes forward, pointing to arr[2].
What is the purpose of the const keyword in: int * const p;?
const after * means the pointer address cannot change, but the data it points to can be modified.
Which scenario demonstrates a dangling pointer?
Dangling pointer occurs when memory is freed but pointer still references that location. This causes undefined behavior.
For function: void modify(int *x) { *x = 20; } If called as modify(&a), what changes?
Passing address allows function to modify original variable. *x = 20 changes the value at that address.
Which of the following is valid pointer comparison?
Pointers of same type can be compared using <, >, ==, !=. Comparing with integers or different types is invalid.
Advertisement
In: int *p; *p = 10; What is the issue?
p is not initialized to valid address. Dereferencing causes undefined behavior (writing to random memory location).
For dynamic 2D array allocation using int arr = (int)malloc(n*sizeof(int*)); what must be done next?
After allocating memory for pointers, each pointer (row) must be allocated memory individually in a loop before accessing arr[i][j].
Which of the following is a valid pointer comparison in C?
Comparing pointers with NULL is valid. Comparing unrelated pointers or subtracting pointers from different arrays is undefined behavior.
What will be the output?
int arr[5] = {1,2,3,4,5};
int *p = arr;
p += 2;
printf("%d", *p);
p points to arr[0]. After p += 2, p points to arr[2] which contains value 3. Pointer arithmetic moves by element size.
In pointer to function declaration: int (*ptr)(int, int); which statement is correct?
The parentheses around *ptr indicate ptr is a pointer. It points to a function that takes two int parameters and returns an int.
Consider a function void modify(int *x) { *x = 20; } called as modify(&y). What happens?
When &y is passed to a function expecting a pointer parameter, the address is passed. Changes via pointer dereference affect the original variable.