Why no output fir this program?

Even though i write print statement it not print in output but if i comment a line free it work fine https://code.sololearn.com/cyix81Ak2IIL/?ref=app

9 Answers

New Answer


Because in line 7 you assign to p a pointer to the string "hello", loosing the path to the allocated memory (a memory leak). Use the strcpy function to assign to the memory pointed by p instead of p itself.(strcpy(p,"hello");)


✳AsterisK✳ No output when working with pointers usually means crash. And it is indeed a crash. Incompatible pointer assignment is happening here. The type of "hello" is const char [6], this is a statically allocated string by the compiler. The type of p is char*, thus invalid pointer is being passed to the free function, causing the crash.


Additionally, assigning p to NULL doesn't solve all problems as the memory leak is still there. Use strcpy as daniel said.


i don't think this is the case of dangling pointer Programmer Raja , ~ swim ~ shed some light please


daniel yeah it's work thanks



Yeah i find why there is no output after spending some time. It is due to dangling pointer If we give p=Null Before we free the memory It's work's fine https://code.sololearn.com/c9cyBYm54ppT/?ref=app


You cant assign the text to 'p' in that manner. Maybe do sprintf(p, "hello"); or strcpy(p, "hello");


Don't use free(p);