2

I'm making a program that has global arrays. In the function where I assign values to the arrays and print them, everything is fine. But as soon as I try to use these arrays in another function, suddenly the values are different.

    int *numeros;
    char *operadores;
    int num, num_operadores;

void crearArray() {

    int i;

    printf("How many numbers?\n");
    scanf("%d", &num);

    numeros = (int*)calloc(num, sizeof(int));
    operadores = (char*)calloc(num - 1, sizeof(char));
    num_operadores = num - 1;

    num += (num - 1);

    for (i = 0; i < num; i++) {

        if(i % 2 == 0 || i == 0) {
            printf("\t\nEnter a number: ");
            scanf("%d", &numeros[i]);
        }
        else {
            fflush(stdin);
            printf("\t\nEnter an operator: ");
            scanf("%c", &operadores[i]);
        }

    }

    printf("Array: ");

    for (i = 0; i < num; i++) {

        if(i % 2 == 0 || i == 0)
            printf("%d ", numeros[i]);

        else
            printf("%c ", operadores[i]);

    }

}


void crearArbol() {

    int i;

    printf("\n\nArrays:\n\t Numbers: ");

    for(i = 0; i < num_operadores + 1; i++)
        printf("\n\t\t Numeros[%d]: %d ", i, numeros[i]);

    printf("\n\t Operators: ");

    for(i = 0; i < num_operadores; i++)
        printf("\n\t\t Operadores[%d]: %c ", i, operadores[i]);

}

int main() {
    crearArray();

    crearArbol();

    return 0;
}

Of course, printing the array wasn't the main purpose for crearArbol but for now there's nothing in there but that and I can't seem to work out why it changes.

Example output:

How many numbers?

3

Enter a number: 1

Enter an operator: *

Enter a number: 2

Enter an operator: /

Enter a number: 3

Array: 1 * 2 / 3 (Printed array from first function crearArray)

Arrays: Numbers:

Numeros[0]: 1

Numeros[1]: 0

Numeros[2]: 2

Operators:

Operadores[0]:

Operadores[1]: * (Printed array values from second function crearArbol)

Thanks in advance for any help!

  • `fflush(stdin);` causes undefined behaviour, [see here](https://stackoverflow.com/questions/2979209/using-fflushstdin) – M.M Jul 24 '18 at 00:51

3 Answers3

2

You're writing and reading outside the bounds of the arrays. You're using i to alternately index numeros and then operadores, but that means i is always twice as big as it should be for either array. You need to divide i by two to convert it into a usable index.

num += (num - 1);

for (i = 0; i < num; i++) {

    if(i % 2 == 0 || i == 0) {
        printf("\t\nEnter a number: ");
        scanf("%d", &numeros[i / 2]);
    }
    else {
        fflush(stdin);
        printf("\t\nEnter an operator: ");
        scanf("%c", &operadores[i / 2]);
    }

}

Rather than doubling num and then alternating access to the two arrays based on whether i is divisible by two, I'd keep num as is and instead access both arrays each loop iteration.

for (i = 0; i < num; i++) {

    printf("\t\nEnter a number: ");
    scanf("%d", &numeros[i]);

    if (i < num - 1)
        fflush(stdin);
        printf("\t\nEnter an operator: ");
        scanf("%c", &operadores[i]);
    }

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

The reason for this behavior is that you are not reading the items into the correct indexes of the array: only indexes 0, 2, 4, ... of numeros have valid data, while indexes 1, 3, 5, ... of oepradores have operators. The remaining indexes have zeros, because you allocated with calloc.

The first function masks this problem by printing the same indexes that it reads, while the second function exposes the issue by printing "straight" indexes.

Obviously, the reading part is an issue, because it goes past the end of allocated arrays (an undefined behavior). You can fix it by modifying the reading loop to read both the number and the operator in the same go:

// Do not increment num
for (i = 0; i < num; i++) {
    printf("\t\nEnter a number: ");
    scanf("%d", &numeros[i]);
    if (i == num-1) {
        break; // Last number comes with no operator
    }
    fflush(stdin);
    printf("\t\nEnter an operator: ");
    scanf("%c", &operadores[i]);
}

Obviously, you would also need to modify the printing code of crearArray to match that of crearArbol.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Respectfully, the mental error was trying to treat two separate arrays as if a single array. Find an example of an array of structures. Use separate index variables on separate arrays?! A suggestion for compound conditionals: use (()) instead of relying on rules of precedent. I really can not figure your use of fflush on an input stream. This also points out the lack of error checking.
As a general practice, learn how to dump your data from your debugger. As a source of debug ideas, save the code, then modify and see what happens...

sys101
  • 1
  • 3