1 c memory string malloc pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr = malloc(sizeof(char) * 1);
ptr = "Hello World";
puts(ptr);
getchar();
}
Run Code Online (Sandbox Code Playgroud)
我不是malloc()专家,但不是那个代码应该给出错误,因为我只分配了一个字节,但是为*ptr指针分配了一个包含11个字节的值?
或者H是否存储在我指定的位置,然后其余的字符串就在它之后的位置?