I'm having some issues with realloc, for example, this code
char *s = (char*)malloc(sizeof(char)*17);
s = "bruhbruhlmaolmao";
s = (char*)realloc(s, sizeof(char)*18);
if(s == NULL) printf("Memory allocation failed");
else printf("%s\n",s);
I'm trying to reallocate the memory chunk that s is pointing at to a larger memory location in the heap.
I've compiled it and the compiler returns no warning. When I run the executable, It prints out nothing. I hope you can explain to me why and how to fix it
s = "bruhbruhlmaolmao";"throws away" the memory you just allocated and instead points the pointer at a string literal. And you cannotrealloca string literals = "bruhbruhlmaolmao";should bestrcpy(s, "bruhbruhlmaolmao");