+ 2

What will be the output of the following C code?

#include <stdio.h> int *f(); int main() { int *p = f(); printf("%d\n", *p); } int *f() { int *j = (int*)malloc(sizeof(int)); *j = 10; return j; }

29th May 2020, 2:40 AM
Rasika Koranne
Rasika Koranne - avatar
4 Respuestas
0
Sorry to say but there will not be any output as I found there is some error in your code. Just check your code again
29th May 2020, 2:45 AM
Piyush
Piyush - avatar
0
All you have to do is just reading error reason: As you can see it says malloc is declared in stdlib library and you fogot to include it
29th May 2020, 3:25 AM
Zia sahebi
0
Just add "#include <stdlib.h>" without quotes below "#include <stdio.h>". This is required to declare the prototype of malloc(). Now the output of the program will be 10: the content of the memory that you allocated and initialized inside the function f() 🤷
29th May 2020, 4:16 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
23rd Jul 2021, 11:12 AM
Yogesh Salgar
Yogesh Salgar - avatar