-1

I'm trying to pass back an updated char array to the original main() function, however when i try to copy over the array with a pointer before it it gives me "Char " differs in levels of indirection from char() [15]'

This is a ISBN validator program.

Here it is:

int main(void)
{
    int divisible;
    char choice, trash;
    char code[15];


    do
    {
        printf("Enter an ISBN (book) number:\n");
        fgets(code, 15, stdin);

        printf("The ISBN entered is:%s\n", code);
        divisible = testISBN(&code);
        if (divisible == 1)
            printf("\n\n\tValid ISBN Number\n");
        else
            printf("\n\n\tInvalid ISBN Number\n");

        printf("\nDo you wish to continue? (Y/N)\n");
        choice = getchar();
        trash = getchar();
    } while (toupper(choice) != 'N');

    return 0;
}


int testISBN(char *code)
{
    int i;
    int sum = 0;
    int weight = 10;
    char codefinal[10];
    int x = 0;
    for (i = 0; i<15; i++)
    {
        int chVal;
        if ((i == 9) && (toupper(code[i]) == 'X'))
        {
            printf("%c",code[i]);
            chVal = 10;
        }
        else
        {
            chVal = code[i] - '0';
        }
        if (chVal == 0 || chVal == 1 || chVal == 2 || chVal == 3 || chVal == 4 || chVal == 5 || chVal == 6 || chVal == 7 || chVal == 8 || chVal == 9) {
            char y = (char)chVal;
            codefinal[x] = y;
            x++;
        }

        sum += chVal * weight;
        weight--;
    }
    printf("sum is %d", sum);

    for  (i = 0; i < 15; i++) {
        *code[i] = codefinal[i];
        printf("%c", code);
    }
    return (sum % 11) == 0;
}

At the very bottom where i said *code[i] = codefinal[i] is where i get the issue. I'm just trying to pass back by pointer my new updated array of numbers to my main.

Jonas
  • 6,915
  • 8
  • 35
  • 53

2 Answers2

0

Just pass code as is it will decay into pointer.

char code[15];
divisible = testISBN(code);

You may want to change testISBN signature to

int testISBN(char code[15]);

This way you have a higher chances to get compiler warning if you mess up indices.

Simple example:

#include <stdio.h>

void func(char arr[3]) {
 arr[0] = 'H';
 arr[1] = 'i';
 arr[2] = 0;
}

int main() {
  char arr[3] = {0};
  func(arr);
  printf("%s", arr);
}

Prints "Hi"


As commenters mentioned your code has other issues such as memory out of bounds access. To ease your life spend some time with your compiler's manual and learn how to turn on all the warnings, they indicate code issues most of the time.

tgregory
  • 554
  • 3
  • 9
0

Do following changes in your code:

  • Change prototype of function int testISBN(char* code) to int testISBN(char code[])
  • Change calling from testISBN(&code); to testISBN(code);
  • Change line *code[i] = codefinal[i]; to code[i] = codefinal[i];
  • In line printf("%c", code); you are printing not the value but the address of code[]. So change it to printf("%c", code[i]);. Note that code[] contains some non printable characters also.
  • Suggestion: You can change line if (chVal == 0 || chVal == 1 || chVal == 2 || chVal == 3 || chVal == 4 || chVal == 5 || chVal == 6 || chVal == 7 || chVal == 8 || chVal == 9) to if((chVal >= 0) && (chVal <= 9)) for simplicity.
cse
  • 4,066
  • 2
  • 20
  • 37
  • So there's an issue where its not storing it as a 'value'? I can't print it at all. – andirewftw Jun 23 '17 at 08:01
  • It's storing it in codefinal incorrectly its supposed to say '0' instead of the null value: http://puu.sh/ws64q/42fc4d9c05.png – andirewftw Jun 23 '17 at 08:04
  • You are printing not the value but the address of `code[]`. I have updated the answer for the same. – cse Jun 23 '17 at 08:22