Array as a function parameter

Asked

Viewed 33 times

1

My program stores 10 numbers in a vector and Zera the numbers that are less than 0. I am receiving these two error messages:

  • Passing argument 1 of 'Zeranegativos' makes Pointer from integer without a cast [-Wint-Conversion]
  • Expected 'int *' but argument is of type 'int'

I’ve tried to put the "and commercial" (&) in the function call and the asterisk (*) in the function parameter to signal a pointer, but it didn’t work either. The code below:

    #include <stdio.h>
     void ZeraNegativos(int entrada[]) {
        for (int i = 0; i < 9; i++) { 
            if (entrada[i] < 0) {
                entrada[i] = 0; //zera todo número menor do que zero
            }
        }
        for (int i = 0; i < 9; i++) {
            printf("%i", entrada[i]);
        }
    }

    int main() {
        int numeros[9];
        for (int i = 0; i < 9; i++) {
            scanf("%i", &numeros[i]); //armazena os números no array
        }   
        ZeraNegativos(numeros[9]);
        return 0;
    }
  • In error message: display the line where the error occurs ?

  • Line 19 and line 3.

  • Stores 9 numbers, not 10. The fact of starting to count at zero, not "sum 1" in amount when you declare int numeros[9]: you reserve space for 9 numbers. Imagine a rule with 9 centimeters: in the mark '9' you are at the end of it, it does not fit anything else. But you can put something between the '0' and '1cm' marks of the ruler: that means count.

2 answers

1

The Zeranegatives function expects an int array and not an int.

Just replace ZeraNegativos(numeros[9]); for ZeraNegativos(numeros);

1


Just a misconception.

#include <stdio.h>
 void ZeraNegativos(int entrada[]) {
    for (int i = 0; i < 9; i++) { 
        if (entrada[i] < 0) {
            entrada[i] = 0; //zera todo número menor do que zero
        }
    }
    for (int i = 0; i < 9; i++) {
        printf("%i", entrada[i]);
    }
}

int main() {
    int numeros[9];
    for (int i = 0; i < 9; i++) {
        scanf("%i", &numeros[i]); //armazena os números no array
    }   
    ZeraNegativos(numeros);
    return 0;
}

Note: you do not need to pass the array number in the language C knows this. Just do this: Zeranegatives(numbers);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.