C Programming Tricky Questions
Tricky Questions on Pointers
What is the difference between NULL pointer and uninitialized pointer?
NULL pointer is explicitly assigned NULL (or 0), pointing to no valid memory. Uninitialized pointer contains garbage value (random memory address). Dereferencing NULL may crash; dereferencing uninitialized pointer causes undefined behavior.
What is a void pointer and what are its limitations?
Void pointer (void*) can point to any data type. Limitations: cannot be dereferenced directly (must be cast first), pointer arithmetic not allowed (size unknown), no type checking at compile time.
Tricky Point: malloc() returns void* because it doesn't know what type of data you'll store.
What is pointer arithmetic and how does it work?
Pointer arithmetic allows adding/subtracting integers to pointers. When you add 1 to pointer, it advances by size of pointed-to type. For int* p, p+1 moves 4 bytes (assuming int is 4 bytes).
What is a dangling pointer and how is it created?
Dangling pointer points to memory that has been freed or gone out of scope. Created by: freeing memory without nullifying pointer, returning address of local variable, or multiple pointers pointing to same freed memory.
Warning: Using dangling pointers causes undefined behavior (crash, data corruption, security vulnerabilities).
What is the difference between array name and pointer?
Array name is constant pointer (cannot be reassigned), sizeof gives total array size. Pointer is variable that can be reassigned, sizeof gives pointer size. Array name decays to pointer in expressions.
What is a wild pointer and how does it differ from dangling pointer?
Wild pointer is uninitialized (contains garbage address). Dangling pointer was valid but now points to freed memory. Both cause undefined behavior when dereferenced.
What is pointer to pointer and when is it useful?
Pointer to pointer (int**) stores address of another pointer. Useful for: modifying pointer in function, dynamic 2D arrays, arrays of strings, and implementing data structures like linked lists.
What is near, far, and huge pointers?
Legacy concepts from 16-bit architecture: near pointers (16-bit, same segment), far pointers (32-bit, different segment), huge pointers (normalized far pointers). Mostly irrelevant in modern 32/64-bit systems.
What is the difference between const pointer and pointer to const?
const pointer: pointer itself is constant (cannot point elsewhere). Pointer to const: pointer can point elsewhere, but cannot modify data through it. const pointer to const: both are constant.
What is function pointer and how is it declared?
Function pointer stores address of function. Declaration: return_type (*ptr_name)(parameter_types). Useful for callback functions, function tables, and implementing polymorphism.
What is pointer aliasing and why is it a problem?
Multiple pointers pointing to same memory location. Problem: compiler optimizations may be disabled (restrict keyword helps), changes through one pointer affect all aliases, can lead to subtle bugs.
What is the restrict keyword in pointer declarations?
restrict keyword tells compiler that pointer is only way to access memory it points to (no aliasing). Allows aggressive optimizations. Violating restrict leads to undefined behavior.
What is pointer subtraction and what does it return?
Subtracting two pointers of same type returns number of elements between them (not bytes). Result is of type ptrdiff_t. Both pointers must point to same array or one past the end.
Can you compare pointers of different types?
Direct comparison of pointers to different types requires explicit cast. Exception: pointers can be compared with NULL (0) or to void* after casting.
What is a smart pointer and does C have them?
Smart pointers automatically manage memory (RAII). C doesn't have built-in smart pointers (C++ feature). In C, you must manually manage memory with malloc/free or implement similar mechanisms.
What is pointer to array vs array of pointers?
Pointer to array: points to entire array. Array of pointers: each element is a pointer. Different declarations: int (*ptr)[10] vs int *arr[10]. Different sizeof results.
What happens when you increment a void pointer?
Pointer arithmetic on void* is not allowed in standard C because size is unknown. Some compilers allow it as extension (treats as char*). Portable code should cast to specific type first.
What is the difference between &arr and &arr[0] for an array?
Both give same address but different types: &arr gives pointer to entire array (int (*)[n]), &arr[0] gives pointer to first element (int*). Important for pointer arithmetic.
What is a null pointer constant vs null pointer?
Null pointer constant is integer 0 (or (void*)0). Null pointer is pointer with that value. In C, NULL is macro for null pointer constant.
Can pointers point to registers?
No, cannot take address of register variable. register keyword suggests variable be stored in register, and registers have no addresses. & operator not allowed on register variables.
What is pointer casting and when is it dangerous?
Casting changes pointer type. Dangerous when: casting to incompatible types (int* to float*), casting away const/volatile qualifiers, or when alignment requirements differ.
What is the size of pointer on 32-bit vs 64-bit systems?
On 32-bit systems: typically 4 bytes. On 64-bit systems: typically 8 bytes. Size of pointer is independent of pointed-to type (all pointers same size on given architecture).
What is a function returning pointer and what are the pitfalls?
Function can return pointer. Pitfalls: returning pointer to local variable (dangling), returning pointer to static/global (thread unsafe), returning pointer to freed memory.
What is pointer to function vs function returning pointer?
Pointer to function: variable that stores function address. Function returning pointer: function that returns pointer type. Different syntax: int (*fp)() vs int* fp().
What is the relationship between pointers and arrays in function parameters?
In function parameters, array decays to pointer. int arr[] is same as int* arr. sizeof gives pointer size, not array size. Need to pass size separately.
Tricky Point: This is why you often see function prototypes with empty brackets for arrays - it's actually a pointer parameter.
Note: These tricky questions cover fundamental and advanced concepts about C pointers, including memory management, pointer arithmetic, common pitfalls, and optimization considerations. Mastering pointers is crucial for writing efficient C code and understanding memory layout.