I have a question regarding the following code
When I run it, I get:Code:/*I omitted checking malloc() for NULL and didnt use free();
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *p = malloc(sizeof *p);
strcpy(p, "Hello, world!");
printf("%s\n", p);
return 0;
}
$gcc -Wall str.c -o str
$./str
Hello, world!
$
The question is if
only allocates on byte, then how come the entire string can be copied to p since the string is like 15 bytes vs one.Code:char *p = malloc(sizeof *p);