我有变量char * amount;
和char t_amount;
我想将金额分配给t_amount。
char * amount = "sdf";
char t_amount = amount;
printf("%s t_amount \n", t_amount);
// I am expecting : sdf t_amount
当我打印时,出现分段错误,我该如何解决这个问题? 希望你们能帮助我。
答案 0 :(得分:0)
变量amount
是指针,因此当char t_amount = amount;
实际上将指针的地址分配给t_amount
变量时,因此会发生段错误
如果t_amount
是char *
,您可以毫无错误地完成该任务。
答案 1 :(得分:0)
char * amount = "sdf";
char t_amount = amount;
printf("%s t_amount \n", t_amount);
amount is a pointer you can not assign that kind of address to a variable. its an error.
when you declare char t_amount = amount, you assign a string to a char because an address is a string.