0
我的char數組允許用戶輸入純數字字符串,從而將每個數字存儲在其自己的數組空間中。我需要將char數組的每個元素分配給int數組中的相應位置。我如何存儲實際的數字,而不是與之相當的ASCII碼用char數組填充char數組(char數組到int數組)
ex。如果我進入9爲字符串,我不想57(ASCII值),但數字9
int main()
{
int x[256] = {0};
int y[256] = {0};
char temp1[256] = {0};
char temp2[256] = {0};
char sum[256] = {0};
printf("Please enter a number: ");
scanf("%s", &temp1);
printf("Please enter second number: ");
scanf("%s", &temp2);
for(i=0; i<256; i++)
{
x[i] = ((int)temp1[i]);
y[i] = ((int)temp2[i]);
}
那輝煌的,但爲什麼我們需要從焦炭減去 '0'?順便說一句,修復它,謝謝 – fifamaniac04 2011-04-27 05:30:59
哦,我想出了爲什麼。當我們將字符9存儲到字符串時,它將其存儲爲ASCII 57,減去字符0(ASCII 48)將導致'9',然後將該值存儲在整數中。真棒再次感謝! – fifamaniac04 2011-04-27 05:37:02