char *someFun1()
{
char temp[ ] = “string";
return temp;
}
char *someFun2()
{
char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};
return temp;
}
int main()
{
puts(someFun1());
puts(someFun2());
}
Answers were Sorted based on User's Feedback
Answer / susie
Answer :
Garbage values.
Explanation:
Both the functions suffer from the problem of dangling
pointers. In someFun1() temp is a character array and so the
space for it is allocated in heap and is initialized with
character string “string”. This is created dynamically as
the function is called, so is also deleted dynamically on
exiting the function so the string data is not available in
the calling function main() leading to print some garbage
values. The function someFun2() also suffers from the same
problem but the problem can be easily identified in this case.
Is This Answer Correct ? | 11 Yes | 0 No |
Variables are in function calls, hence on heap space. Variable address is being returned by function and when function returns the allocated space is freed.
Now accessing returned address will result in : segmentation fault.
As address no longer allocated to function, results in invalid address accessed by process.
As variable address does not exist ( hence variable address also , as heap is freed).
Is This Answer Correct ? | 0 Yes | 0 No |
main() { int i=10; i=!i>14; Printf ("i=%d",i); }
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
main() { int a[10]; printf("%d",*a+1-*a+3); }
source code for delete data in array for c
what is the code of the output of print the 10 fibonacci number series
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024
Under linux environment can u please provide a c code for computing sum of series 1-2+3-4+5......n terms and -1+2-3+4-5...n terms..
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);
How we print the table of 3 using for loop in c programing?
main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }
What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }