gpt4 book ai didi

c - 警告 : passing argument 1 of ‘calculation’ makes pointer from integer without a cast [enabled by default]

转载 作者:行者123 更新时间:2023-11-30 17:08:30 28 4
gpt4 key购买 nike

我不断收到此错误,但找不到问题所在:

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’

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

#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,则程序结束。程序将值传递给计算函数,计算函数计算数组并​​显示总值。

最佳答案

如果要将数组作为函数参数发送,则必须发送数组的基地址并使用指向相同类型的指针接收它。类似的东西

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

calculation(myArray[i], myArrayTwo[i], i);
^^^^^^^^^^ ^^^^^^^^^^^^^
int not int*
<小时/>

对于数组myArray[50],它的名称myArray代表数组的基地址,所以你可以使用这样的东西

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)

关于c - 警告 : passing argument 1 of ‘calculation’ makes pointer from integer without a cast [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com