1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What happens if a function returns the address of one of…

Added 30 Nov -0001

What happens if a function returns the address of one of its local variables?

  1. The compiler promotes the variable to global
  2. The program stops with a runtime error
  3. Using that pointer is undefined behaviour Correct answer
  4. The value is moved to the heap automatically

Correct answer

C. Using that pointer is undefined behaviour

14 views

MCQSEXAM

Explanation

The local object's lifetime ends on return, so the pointer dangles and any use of it is undefined behaviour.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs