← Quiz: Storage Classes in C: all 15 questions
Quiz: Storage Classes in C · Question 10 of 15
A function returns the address of its local array char buf[20]; (not static). What is the problem?
A function returns the address of its local array char buf[20]; (not static). What is the problem?
- A.None — arrays are always global
- B.The array is copied, which is slow
- C.buf is destroyed when the function returns, so the caller gets a dangling pointer
- D.It does not compile in any compiler
Answer & explanation
Correct answer: buf is destroyed when the function returns, so the caller gets a dangling pointer
An auto local dies with its function. Make it static, use malloc, or let the caller pass a buffer.
Can you get the rest right without peeking?
Take the timed Quiz: Storage Classes in C quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →