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? Unknown Category programming-in-c output + – vaishali jhalani asked Nov 9, 2016 vaishali jhalani 1.3k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
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 Arjun answered Nov 9, 2016 • selected Nov 9, 2016 by Digvijaysingh Gautam Arjun comment Share Follow See all 2 Comments See all 2 2 Comments reply vaishali jhalani commented Nov 9, 2016 reply Follow Share str = fun(); what is meaning of this line? 0 votes 0 votes Digvijaysingh Gautam commented Nov 9, 2016 reply Follow Share see fun() is returning starting address of arr. So this address will be stored in str 0 votes 0 votes Please log in or register to add a comment.
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" Digvijaysingh Gautam answered Nov 9, 2016 Digvijaysingh Gautam comment Share Follow See all 0 reply Please log in or register to add a comment.