2017-03-29 62 views
-1

我有變量char * amount;char t_amount; 我想將金額分配給t_amount。如何使用C分配char *值到char使用C

char * amount = "sdf"; 
char t_amount = amount; 
printf("%s t_amount \n", t_amount); 
// I am expecting : sdf t_amount 

當我打印,發生分割錯誤,所以我如何解決這個問題? 希望你們能幫助我。

+0

爲什麼要使用't_amount'而不是直接使用'amount'(或另一個'char *')? –

+1

你想做什麼?你的代碼沒有意義 –

+2

'char t_amount [MAX_LENGTH + 1]; strcpy(t_amount,amount);' – BLUEPIXY

回答

0

變量amount是一個指針,所以當char t_amount = amount;你實際上是分配指針的地址給你t_amount變量,因此段錯誤 如果t_amountchar *你可以做那項任務沒有任何錯誤。

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.