代码打印数组值的地址而不是值?
Code prints the address' of the array values and not the values?
为什么这段代码打印的是 int 数组的地址而不是值?
假设 str
是“2002 年 8 月 18 日”,它会打印:
total digits: 6 all digits list: 49 56 50 48 48 50
当我想让它说:
total digits: 6 all digits list: 1 8 2 0 0 2
这是代码的相关部分。
函数:
int extractDigits (char str[MAX], int digitsInInputString[MAX]) { //returns total digits in str, and appends digitsInInput to list of digits
int i;
int totalDigits = 0;
for (i=0; i < strlen(str) - 1; i++) {
if (isdigit(str[i])) {
digitsInInputString[totalDigits] = str[i];
totalDigits++;
}
}
return totalDigits;
}
主程序:
digitsInString = extractDigits(myStr, strNums);
printf("total digits: %d all digits list:", digitsInString);
for (i=0; i<digitsInString; i++) {
printf(" %d", strNums[i]);
}
printf("\n");
将这一行 digitsInInputString[totalDigits] = str[I];
重写为:
digitsInInputString[totalDigits] = str[I] - '0';
这会将 ASCII 字符转换为十进制。
为什么这段代码打印的是 int 数组的地址而不是值?
假设 str
是“2002 年 8 月 18 日”,它会打印:
total digits: 6 all digits list: 49 56 50 48 48 50
当我想让它说:
total digits: 6 all digits list: 1 8 2 0 0 2
这是代码的相关部分。
函数:
int extractDigits (char str[MAX], int digitsInInputString[MAX]) { //returns total digits in str, and appends digitsInInput to list of digits
int i;
int totalDigits = 0;
for (i=0; i < strlen(str) - 1; i++) {
if (isdigit(str[i])) {
digitsInInputString[totalDigits] = str[i];
totalDigits++;
}
}
return totalDigits;
}
主程序:
digitsInString = extractDigits(myStr, strNums);
printf("total digits: %d all digits list:", digitsInString);
for (i=0; i<digitsInString; i++) {
printf(" %d", strNums[i]);
}
printf("\n");
将这一行 digitsInInputString[totalDigits] = str[I];
重写为:
digitsInInputString[totalDigits] = str[I] - '0';
这会将 ASCII 字符转换为十进制。