使用指针的所有功能的警告消息.预期为"int *",但参数的类型为"int(*)[10]" [英] Warning messages for all of my functions using pointers. Expected 'int*' but argument is of type 'int (*)[10]'

查看:139
本文介绍了使用指针的所有功能的警告消息.预期为"int *",但参数的类型为"int(*)[10]"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码运行正常,我只是担心收到的警告消息,是否有办法使它们不出现?有什么理由担心他们吗?在代码的更深处,我不太了解自己的工作或工作原理.以前没有用,所以我查了其他人对指针的处理,现在可以了

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

就在这里,为什么我不能使用'a'而不是'a + 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);
        }
}

推荐答案

我认为错误消息是很容易描述的.

I think the error message is pretty self-describing.

在您的代码中,a是具有int [10]的数组类型.您将&a(其类型为 pointer的指针)传递给10个int s 的数组,或者将int (*)[10]的类型与指向int的指针的类型不同,即int *.因此,编译器会尖叫.

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

这篇关于使用指针的所有功能的警告消息.预期为"int *",但参数的类型为"int(*)[10]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆