Which pointer operation is NOT valid in C?
Pointer multiplication is not allowed. Valid operations are addition, subtraction, increment, decrement, and comparison.
What is the output?
int arr[2][3] = {{1,2,3}, {4,5,6}};
int *p = (int*)arr;
printf("%d", *(p+4));
Array flattened: 1,2,3,4,5,6. p+4 points to 5th element which is 5.
What does void *ptr represent?
void* is a generic pointer. It can be cast to any other pointer type. No arithmetic can be done directly.
What is the output?
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
swap() exchanges values through pointers. x becomes 10, y becomes 5 after function call.
Consider: int arr[] = {1,2,3,4,5}; int *p = arr + 2; What is arr - p?
arr points to arr[0], p points to arr[2]. arr - p = -2 (p is 2 positions ahead).
Advertisement
What is malloc(0) likely to return?
malloc(0) behavior is implementation-defined but typically returns a valid pointer. Freeing it is safe.
What is the output?
char str[] = "ABC";
char *p = str;
printf("%c", *(p+2));
p+2 points to str[2] which is 'C'. Note: str[3] is null terminator.
Which of the following is correct about pointer assignment?
int *p, *q;
p = q; // What happens?
p = q copies the address stored in q to p. Now both point to same location.
What is the output?
int x = 5;
int *const p = &x;
x = 10;
printf("%d", *p);
const pointer p cannot be changed, but x can be modified. *p prints current value of x which is 10.
What will be printed?
int *p = (int*)malloc(5 * sizeof(int));
int *q = p;
p = NULL;
free(q);
q still holds the address even though p is NULL. free(q) properly deallocates memory.
What is the difference between arr and &arr if arr is an array?
int arr[5];
arr decays to pointer to first element (int*). &arr is pointer to whole array (int(*)[5]). Pointer arithmetic differs.
What is the output?
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d", sizeof(p));
p is a pointer, so sizeof(p) returns pointer size (8 bytes on 64-bit, 4 on 32-bit), not array size.
What is the size of a pointer in a 64-bit system?
In 64-bit systems, pointers are 8 bytes (64 bits) regardless of the data type they point to.
Consider the code: int x = 10; int *p = &x; int q = &p; What is the value of q?
q points to p, p points to x. **q dereferences twice: first gets p's value (address of x), second gets x's value (10).
Which operation is NOT allowed on void pointers in C without explicit casting?
Void pointers cannot perform pointer arithmetic (++, --, +n) directly. They must be cast to a specific type first.
What is the difference between NULL and a wild pointer?
NULL is explicitly set to address 0, while wild pointers contain garbage values from uninitialized memory.
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.
How does free() handle a NULL pointer?
free(NULL) is safe in C and does nothing. This is by design to prevent errors when freeing pointers.
Which scenario demonstrates a dangling pointer?
Dangling pointer occurs when memory is freed but pointer still references that location. This causes undefined behavior.