C Pointers - Tricky MCQ

Tricky Questions on Pointers

1

What is the size of a pointer in C?

Correct Answer: C) Depends on architecture and data type

Pointer size depends on system architecture (4 bytes on 32-bit, 8 bytes on 64-bit typically). However, all pointers to data types have same size on given architecture. Function pointers may have different sizes. Pointer size is unrelated to pointed data size.

2

What is NULL pointer in C?

Correct Answer: D) Both A and C

NULL is a macro defined as ((void*)0), representing pointer pointing to address 0. It indicates pointer doesn't point to valid memory. Dereferencing NULL causes segmentation fault. Uninitialized pointers contain garbage values, not NULL.

3

What is pointer arithmetic?

Correct Answer: D) All of the above

Pointer arithmetic automatically scales by sizeof(type). Adding 1 to int* moves 4 bytes ahead (on 32-bit). Subtracting pointers gives number of elements between them. Invalid operations: adding two pointers, multiplying/dividing pointers.

4

What is void pointer (void*)?

Correct Answer: D) All of the above

void* is generic pointer type. Must be cast to specific type before dereferencing. Pointer arithmetic not allowed because size of void is unknown. Used in generic functions like qsort(), malloc() return type.

5

What is dangling pointer?

Correct Answer: D) All of the above

Dangling pointer points to memory that is no longer valid (freed, out of scope, reallocated). Dereferencing causes undefined behavior - crash, garbage values, or seemingly correct results. Always set pointer to NULL after free().

6

What is wild pointer?

Correct Answer: D) All of the above

Wild pointer is uninitialized, containing random memory address. More dangerous than NULL because it might point to valid memory, causing silent corruption. Always initialize pointers: int *p = NULL; or int *p = &var;

7

What is function pointer?

Correct Answer: D) All of the above

Function pointer stores address of function. Syntax: return_type (*ptr_name)(parameters). Used for callbacks, function tables, strategy pattern. Must match exact signature. Calling through pointer: ptr(args) or (*ptr)(args).

8

What is difference between ++*p, *p++, and *++p?

Correct Answer: D) All of the above

++*p increments pointed value. *p++ dereferences current pointer, then pointer increments. *++p increments pointer first, then dereferences new address. Operator precedence: postfix ++ > * > prefix ++. Common source of bugs.

9

What is pointer to pointer (double pointer)?

Correct Answer: D) All of the above

int **pp stores address of int* pointer. Needed when function must modify original pointer (like realloc). Used for dynamic array of pointers (2D arrays). Dereferencing: *pp gives pointer, **pp gives value.

10

What is const with pointers?

Correct Answer: D) All of the above

const applies to whatever is on its left (or right if nothing on left). const int *p: can change pointer, cannot change value. int *const p: cannot change pointer, can change value. const int *const p: cannot change either.

11

What is near, far, and huge pointers?

Correct Answer: D) All of the above

From 16-bit x86 with segmented memory. Near: 16-bit offset within segment. Far: 32-bit (segment:offset). Huge: normalized far pointers. Obsolete in 32/64-bit flat memory models. Modern compilers ignore these keywords.

12

What is smart pointer in C?

Correct Answer: D) All of the above

C lacks built-in smart pointers like C++'s unique_ptr/shared_ptr. Can simulate with struct containing pointer and reference count. Requires manual implementation of RAII patterns. Still needs careful memory management.

13

What is pointer aliasing?

Correct Answer: D) All of the above

Aliasing occurs when same memory accessed through different pointers. Compilers must assume aliasing could happen, limiting optimizations. restrict keyword (C99) promises no aliasing, enabling better optimization. Violating restrict causes undefined behavior.

14

What is difference between array and pointer?

Correct Answer: D) All of the above

Array name decays to pointer in expressions but isn't identical. Array has contiguous memory allocation. Pointer stores address, needs separate memory. Cannot assign to array name, can assign to pointer. Array knows its size (in declaring scope).

15

What is pointer to array vs array of pointers?

Correct Answer: D) All of the above

Operator precedence: [] has higher precedence than *. int *p[10] is array of pointers ([] binds first). int (*p)[10] is pointer to array (parentheses change binding). Crucial difference in declaration and usage. Common source of confusion.