Skip to content
Related Articles

Related Articles

C | Storage Classes and Type Qualifiers | Question 19
  • Difficulty Level : Medium
  • Last Updated : 09 Sep, 2013




#include <stdio.h>
char *fun()
{
    static char arr[1024];
    return arr;
}
  
int main()
{
    char *str = "geeksforgeeks";
    strcpy(fun(), str);
    str = fun();
    strcpy(str, "geeksquiz");
    printf("%s", fun());
    return 0;
}


(A) geeksforgeeks
(B) geeksquiz
(C) geeksforgeeks geeksquiz
(D) Compiler Error


Answer: (B)

Explanation: Note that arr[] is static in fun() so no problems of returning address, arr[] will stay there even after the fun() returns and all calls to fun() share the same arr[].

    strcpy(fun(), str);  // Copies "geeksforgeeks" to arr[]
    str = fun();    // Assigns address of arr to str
    strcpy(str, "geeksquiz");  // copies geeksquiz to str which is address of arr[]
    printf("%s", fun());   // prints "geeksquiz"


Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :