C中的错误malloc

Tha*_*yen 0 c malloc

我不确定这是错误的它说不兼容隐式内置func我确实包括string.h文件但它仍然给我一个错误

 int name_read;
 int name_bytes = 100;
 char *name;

 printf("Please enter name:\n");
 name = (char *)malloc(name_bytes + 1);
 name_read = getline(&name, &name_bytes, stdin);
Run Code Online (Sandbox Code Playgroud)

Joh*_*ter 8

你需要#include <stdlib.h>得到正确的声明malloc.

也sizeof(name_bytes) + 1看起来很腥; 这将给你5个字节的内存,而不是你可能预期的101.

最后,不需要malloc在C中转换返回值,因为它返回a void*.

#include <stdlib.h>
/* ... */
int name_bytes = 100;
char* name = malloc(name_bytes + 1);
Run Code Online (Sandbox Code Playgroud)