Machine-translated by Google. Questions are authored in English.
Top

Discussion

The output of the code below is

int *m();
    void main()
    {
        int k = m();
        printf("%d", k);
    }
    int *m()
    {
        int a[2] = {5, 8};
        return a;
    }

 

  • A.5
  • B.8
  • C.Nothing
  • D.Varies

Answer: D

Correct answer: Varies

Two faults compound each other. The function returns a pointer to a local array that no longer exists, and the caller assigns that pointer to an int. What is printed is an address reinterpreted as an integer - different on every run and every machine.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.