A pointer holds the address of another object. The address-of operator & and dereference operator * are central to C—compare with references in Rust or object references in Java, but without automatic safety checks.
Declaration and use
int value = 10;
int *ptr = &value; /* ptr stores address of value */
*ptr = 20; /* writes through pointer */
Null pointers
Initialize unused pointers to NULL (from stddef.h). Always check before dereferencing pointers that might be null.
Important interview questions and answers
- Q: What is NULL?
A: A null pointer constant representing no object—dereferencing it is undefined behavior. - Q: int* p vs int *p?
A: Same meaning; style guides differ—be consistent:int *pemphasizes that*pis anint. - Q: Why use pointers?
A: Modify caller data, avoid copying large structs, dynamic allocation, and data structures like linked lists.
Self-check
- What operator gets an address?
- What operator reads through a pointer?
Pitfall: Dereferencing uninitialized or NULL pointers crashes or triggers undefined behavior—compare with Java where null checks throw NPE instead of UB.
Interview prep
- What is NULL?
A null pointer constant representing no object; dereferencing it is undefined behavior.
- Why use pointers?
Modify caller data, avoid copying large structs, dynamic allocation, and build linked data structures.