我对以下程序有些困惑。
#include<stdio.h>
void main()
{
char *string;
string = (char *)malloc(5);
string = "abc"; // <<<<<<<<<< why *string="abc" is not working. How string = "abc" is working?
printf("%s", string);
}
但是使用整数的相同程序可以正常工作。
char *i;
i=(int *)malloc(sizeof(int));
*i=4; <<<<<<<< this is working fine
printf("%d",*i);
int main(void)
而不是void main()
。 - Sourav Ghoshmalloc()
及其相关函数的返回值进行强制转换。 - Sourav Ghosh