如何使用C将char *值赋给char

时间:2017-03-29 04:38:05

标签: c

我有变量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

当我打印时,出现分段错误,我该如何解决这个问题? 希望你们能帮助我。

2 个答案:

答案 0 :(得分:0)

变量amount是指针,因此当char t_amount = amount;实际上将指针的地址分配给t_amount变量时,因此会发生段错误 如果t_amountchar *,您可以毫无错误地完成该任务。

答案 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.