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.
What is the result of: int x = 5; int *p = &x; int *q = p; if p == q?
Both p and q point to same address of x, so p == q evaluates to true (1).
In pointer to function: int (*ptr)(int, int); What does this declare?
Parentheses around *ptr give pointer priority. It's a pointer to a function taking 2 ints and returning int.
What happens with: int arr[10]; int *p = arr; *p = 5; printf("%d", arr[0]);?
p points to arr[0]. *p = 5 modifies arr[0]. Printing arr[0] displays 5.
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).
What is the output of: printf("%p", NULL);?
%p prints pointer values. NULL representation depends on implementation, commonly shown as 0x0 or (nil).
For dynamic 2D array: int arr = (int)malloc(n * sizeof(int*)); What's missing?
This allocates row pointers only. Each row needs allocation: arr[i] = (int*)malloc(m * sizeof(int));
What is the relationship between arrays and pointers?
Array name acts as pointer to first element in expressions but they're distinct. &arr and arr differ in type.
Consider: const int *p; and int * const q; Which statement is true?
const int *p: pointer can change, data cannot. int * const q: pointer cannot change, data can be modified.
What is the output of the following code?
int x = 10;
int *p = &x;
int q = &p;
printf("%d", q);
q is a pointer to pointer p. **q dereferences q twice: first to get p, then to get the value of x which is 10.
Consider the declaration: const int *p; and int * const q;
Which statement is TRUE?
const int *p means p can change but the data it points to cannot. int * const q means q cannot change but the data it points to can be modified.
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].
What is the primary issue with this code?
int *p;
*p = 10;
p is declared but not initialized to point to any valid memory location. Dereferencing an uninitialized pointer causes undefined behavior.
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.