1,295 views
1 votes
1 votes
#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;
}

What would be the output?

2 Answers

Best answer
3 votes
3 votes
char *str = "geeksforgeeks";
//a RO memory is created and "geeksforgeeks"
//stored there and address assigned to str
strcpy(fun(), str);
//fun returns the address of arr and content
//of str copied there.
str = fun();
//value of str changes to that of arr
strcpy(str, "geeksquiz");
//"geeksquiz" copied to memory pointed to by atr(arr)
printf("%s", fun());
//Content from memory pointed to by arr printed
selected by
2 votes
2 votes
arr is a static array so it's content will remain in the memory even after execution of the function fun()

str = fun(); ---------------- becoz of this statement str contains address of arr

strcpy(str, "geeksquiz"); -------------------- now "geeksquiz" will be stored in str which is pointing to arr

printf("%s", fun()); ------------------- so if we try to print arr we will get "geeksquiz"

Related questions

2 votes
2 votes
1 answer
1
manisha11 asked Sep 13, 2018
649 views
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?A) (char*) (*ptr[N])( )(B) (char*(*)) (*ptr[N])( )(C) (char*(...