-->

Warning messages for all of my functions using poi

2020-05-07 06:30发布

问题:

The code works fine I'm just worried about the warning messages I'm getting, would there be a way to make them not appear? Is there any reason to be worried about them? Also farther down in the code I don't quite understand what I did or why it worked. It wasn't working before so I looked up what other people did with pointers and it works now

Warning passing argument 1 of 'readarray' from incompatible pointer type [-wincomp
readarray(&a);
          ^
note: expected 'int*' but argument is of type 'int(*)[10]'
void readarray (int*);
     ^

This is the warning message, I get it for each of my functions^^^^^^^^^^^^^^^^^^

I understand why it's having issues, I think, but I don't understand how I could change anything

#include <stdio.h>
#define n 10

void readarray (int*);
int findmaxvalue(int*);
void reversearray(int*, int*);
void printarray(int*);

int main(void)
{
        int a[n], i, b[n];

        for (i = 0; i < n; i++)
        {
                a[i] = 0;
        }
        for (i = 0; i < n; i++)
        {
                b[i] = 0;
        }

        readarray(&a);
        findmaxvalue(&a);
        reversearray(&a, &b);
        printarray(&b);

    return 0;
    }


void readarray (int *a)
{
        int *q;

        q = a;
        printf("Enter up to 10 numbers. Terminate by entering a 0\n");

Right here, why can't I use 'a' instead of 'a+n'

        for(q = a; q < a+n; q++)
        {
                scanf("%d", q);
                if (*q == 0)
                        break;
        }
        printf("\n");
}


int findmaxvalue(int *a)
{
        int i, max;

        max = a[0];
        for (i = 1; i < n; i++)
        {
                if (a[i] > max)
                        max = a[i];
        }
        printf("The highest element in the array is: %d\n\n", max);
        return max;
}


void reversearray(int *a, int *b)
{
        int *i, *j, t;

        for (i = a; i < a+n; i++)
        {
                for (j = i + 1; j < a+n; j++)
                {
                        if (*j < *i)
                        {
                                t = *j;
                                *j = *i;
                                *i = t;
                        }
                }
        }

        for (i = a + n - 1, j = b; i > a; i--, j++)
        {
                *j = *i;
        }
}

void printarray(int *b)
{
        int *q;

        q = b;
        printf("The reversed array in descending order is:\n");
        for (q = b; q < b+n; q++)
        {
                printf("%d  ", *q);
        }
}

回答1:

I think the error message is pretty self-describing.

In your code, a is an array type, having int [10]. You pass &a, which is of type pointer to an array of 10 ints, or, int (*)[10] which is not the same type as a pointer to int, i.e., int *. Hence the compiler screams.

As array type variables decay to the pointer to the first element of the array while passed as function arguments, you should call your function like

 readarray(a);