This my code in C , I am trying to change the array size dynamically but it get me to null pointer.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *message = {"xxzz"};
message=realloc(message,5 * sizeof(*message));
if ( message == NULL)printf("Memory exhausted\n");
printf("%s",message);
return 0;
}
You can only pass an pointer to
reallocwhich was allocated memory usingmallocorcallocor other memory allocating functions.Failing to do so results in Undefined Behavior, that is what you have in your code.