I need to realloc a string acquired via scanf("%ms", ...)
, does realloc
automatically include the termination character \0
in my reallocated string? What's the behavior of realloc
in this case?
Will it add \0
at the end of the reallocated string, or will it leave the \0
in the same position of the previous string, adding uninitialized memory after \0
?
For example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *string = NULL;
char *p = NULL;
int length = 0;
//This should automatically add \0 at the end, if i'm not wrong
scanf("%ms", &string);
length = strlen(string);
p = realloc(string, sizeof(char) * (length + 10));
if (p != NULL) {
string = p;
p = NULL;
}
free(string);
return 0
}
PS: I used strlen()
over the string like here: strlen with scanf("%ms"...)