Warning messages for all of my functions using pointers. Expected 'int*' but argument is of type 'int (*)[10]'

偶尔善良 提交于 2020-01-11 14:15:09

问题


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);


来源:https://stackoverflow.com/questions/43222269/warning-messages-for-all-of-my-functions-using-pointers-expected-int-but-arg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!