Search code examples
cundefined-behaviorrealloc

Why am I getting error when calling realloc on a char[]?


I have char[] as such

char string[] = "Hello world !!!";

when I'm trying to reallocate it's memory I'm getting error

realloc(string, 50); // error here

Solution

  • You can only realloc things you malloc/calloc/realloc'ed. A char array is not any of those things, so it is normal for you to get an error in this case.