我有下面的程序如何将字符串存储到字符指针?
#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主要(无效)''以上无效的主要()' –
标准警告:请[不要投(http://stackoverflow.com/q/605845/ 2173917)'malloc()'和'C'中的家族的返回值。 –