I have a question regarding the following code
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;
}
When I run it, I get:
$gcc -Wall str.c -o str
$./str
Hello, world!
$
The question is if
Code:
char *p = malloc(sizeof *p);
only allocates on byte, then how come the entire string can be copied to p since the string is like 15 bytes vs one.