Multiple choice technology programming languages

A friend of mine once wrote a function as follows. int * func() { int i; for (int k=0;k<200;k++) //Do something here .... return &i; } I think it is faulty . What is wrong here?

  1. Nothing is wrong. You are wrong.

  2. Loops are not allowed inside the function scope, Since it degrades performance

  3. My friend tried to return a pointer to a local function variable , which is invalid once the function call compltes.

  4. The value i is not properly initialised, SO it may contain some garbage .

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The function returns a pointer to a local variable i. Local variables have automatic storage duration and are destroyed when the function returns. The returned pointer becomes a dangling pointer pointing to invalid memory. Using this pointer causes undefined behavior. This is a classic C/C++ error - never return pointers to local variables.