【发布时间】:2015-11-02 10:13:48
【问题描述】:
我需要在屏幕上打印一行然后获取用户输入,但是printf("blah") 语句导致我的代码无法编译。错误消息显示“char not expected”,但是当我将printf() 语句注释掉时,代码会编译。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Welcome to the shell!\n");
printf("shell:>");
char* inp = (char*)malloc(20); // error at this line
}
我在MINIX 3.1.0中使用cc编译器
【问题讨论】:
-
请see why not to cast
malloc()和C中的family返回值。 -
并包含
stdlib.h,不要忘记释放分配的内存。 -
我原来的程序里有的,我刚才加到这里了。稍后在程序中释放内存。 :-) 我现在将检查 malloc() 链接。谢谢。