Home > Net >  Code prints the address' of the array values and not the values?
Code prints the address' of the array values and not the values?

Time:10-25

Why is this code printing the addresses of the int array and not the values? Say str is "Aug 18, 2002", it prints this:

total digits: 6 all digits list: 49 56 50 48 48 50

When I want it to say:

total digits: 6 all digits list: 1 8 2 0 0 2

Heres the relevant parts of the code.

Function:

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;
}

Main program:

        digitsInString = extractDigits(myStr, strNums);
        printf("total digits: %d all digits list:", digitsInString);
        for (i=0; i<digitsInString; i  ) {
                printf(" %d", strNums[i]);
        }
        printf("\n");

CodePudding user response:

Rewrite this line digitsInInputString[totalDigits] = str[I]; to be:

digitsInInputString[totalDigits] = str[I] - '0';

This will convert from ASCII characters to decimal.

  • Related