passing argument makes pointer from integer

后端 未结 4 1374
猫巷女王i
猫巷女王i 2020-12-19 06:34

I can\'t find my problem. keeps giving me these errors:

\"c:2:5: note: expected \'int *\' but argument is of type \'int\'\"
\"c:28:1: warning: passing argume         


        
相关标签:
4条回答
  • 2020-12-19 07:10

    The answer to your question is to swap this:

    CountEvenNumbers(numbers[length], length);
    

    for this

    CountEvenNumbers(numbers, length);
    

    However, if you continue with coding, a skill you might find invaluable is deciphering warrning/error messages:

    "c:2:5: note: expected 'int *' but argument is of type 'int'"
    "c:28:1: warning: passing argument 1 of 'CountEvenNumbers' makes pointer from integer without a cast [enabled by default]"

    So what does that mean? It states that on line 28 (CountEvenNumbers( numbers[length] , length ); ) it expected you to make a cast of argument 1, meaning you passed it something that it did not expect. So you know something is wrong with the first argument.

    The trick here is the other line: expected 'int *' but argument is of type 'int' It's saying "I wanted a pointer to an integer, but you gave me just an integer". That's how you know you're passing the wrong type.

    So what you should be asking yourself is, what type is argument 1? You know if you want to access an element inside the array you need to use the []'s, (you did so on lines 20 and 25 of your code), so by passing numbers[length] to your function, your trying to pass it a single element1 instead of a full array like it expects.

    The other half of this is expected 'int *', why would your function expect to get a pointer to an int? Well that's because in C, when you pass an array of (type) it decays to a pointer to (type).

    1 of course numbers[length] isn't really an element in your array anyway, it overflows it.

    0 讨论(0)
  • 2020-12-19 07:13

    On line 28, you're trying to pass the integer at index "length" of numbers. You should just pass numbers itself, so something like CountEvenNumbers(numbers, length);

    0 讨论(0)
  • 2020-12-19 07:16

    Read a C tutorial, really. array[index] subscripts/indexes the array, and thus it yields the indexth element in the array. If you want to pass the array to be operated on itself (well, rather a pointer to its first element), then simply write its name:

    CountEvenNumbers(numbers, length);
    
    0 讨论(0)
  • 2020-12-19 07:33

    Try this.

    #include <stdio.h>
    #include <stdlib.h>
    
    int CountEvenNumbers(int numbers[], int length);
    int main(void)
    {
        int length;
        int X;int Z; int Y; int W;
        X=0;Y=0;Z=0;W=0;
        printf("Enter list length\n");
        scanf("%d",&length);
    
        int *numbers = (int*) calloc(length, sizeof(int)); //***************
    
    
        if (length<=0)
        {printf("sorry too low of a value\n");
        return 0;}
        else
        {
            printf("Now, enter %d integers\n",length);
            for (X=0;X<length;X++)
            {scanf("%d",&Y);//X is position in array, Y is value.
            numbers[X]=Y;
            }
            printf("The list reads in as follows:\n");
            for (W=0;W<length;W++)
            {Z=numbers[W];
            printf("%d ",Z);}
            printf("\n");
        }
        CountEvenNumbers( numbers , length ); //**************
        free (numbers);
        return 0;
    }
    
    int CountEvenNumbers(int numbers[], int length)
    {
        int odd_count;int even_count;int P;int Q;
        Q=0; odd_count=0;even_count=0;
        for (P=0;P<length;P++)
            if (numbers[Q]==0)
            {even_count++;
        Q++;}
            else if ((numbers[Q]%2)!=0)
            {odd_count++;
        Q++;}
            else
            {even_count++;
            Q++;}
            printf("There are %d even numbers in the series\n",even_count);
            return 0;
    }
    
    0 讨论(0)
提交回复
热议问题