警告:传递“ calculation”的参数1会使指针从整数开始而无需强制转换[默认启用] [英] warning: passing argument 1 of ‘calculation’ makes pointer from integer without a cast [enabled by default]

查看:69
本文介绍了警告:传递“ calculation”的参数1会使指针从整数开始而无需强制转换[默认启用]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到此错误,但找不到问题:

I keep getting this error and I can't find the problem:

exam.c: In function ‘main’:
exam.c:21:2: warning: passing argument 1 of ‘calculation’ makes pointer from integer without a cast [enabled by default]
exam.c:2:5: note: expected ‘int *’ but argument is of type ‘int’
exam.c:21:2: warning: passing argument 2 of ‘calculation’ makes pointer from integer without a cast [enabled by default]
exam.c:2:5: note: expected ‘int *’ but argument is of type ‘int’

我试图将数组更改为指针,但这也不起作用。这是我的代码:

I tried to change the arrays to pointers, but that did not work either. Here is my code:

#include <stdio.h>
int calculation(int arrayOne[50], int arrayTwo[50], int i);
int main(void) {

        int myArray[50];
        int myArrayTwo[50];
        int i;

        for(i=0;i<49;i++) {

                printf("Enter values of arrays: ");
                scanf("%d", &myArray[i]);
                printf("Enter second value: ");
                scanf("%d", &myArrayTwo[i]);
                if (myArray[i] == 0) {

                        break;
                }
        }

        calculation(myArray[i], myArrayTwo[i], i);
}

int calculation(int arrayOne[50], int arrayTwo[50], int i) {

        int total;
        int j;

        for (j=0;j<i;j++) {

                total = arrayOne[j] + arrayTwo[j];
        }
}

我要做的是创建一个程序可以容纳50个不同的值,如果用户为myArray输入0,则程序结束。
程序将值传递给计算函数,然后计算函数计算数组并​​显示总值。

What I am trying to do is create a program which can hold 50 different values and if a user enters 0 for myArray then the program ends. The program passes the values to calculation function and calculation functions calculates the arrays and show the total value.

推荐答案

如果您要发送 array 作为函数参数,则必须发送 array 的基地址并以指向相同类型的指针。像这样的

If you want to send array as function parameters, you have to send the base address of the array and receive it with a pointer to the same type. Something like

您将其作为指针接收,但是您发送的是 int 而不是指针的基址数组,

You are receiving it as a pointer, but you are sending int instead of the base address of the array,

calculation(myArray[i], myArrayTwo[i], i);
            ^^^^^^^^^^  ^^^^^^^^^^^^^
                  int not int*






对于数组 myArray [50] 其名称 myArray 表示数组的基地址,因此您可以使用类似的东西


For an array myArray[50] its name myArray represents the base address of the array, so you can use something like this

calculation(myArray, myArrayTwo, i); //myArray is the base address of myArray[] array
.
.
int calculation(int* arrayOne, int* arrayTwo, int i)
//OR
int calculation(int arrayOne[], int arrayTwo[], int i)
//OR
int calculation(int arrayOne[50], int arrayTwo[50], int i)

这篇关于警告:传递“ calculation”的参数1会使指针从整数开始而无需强制转换[默认启用]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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