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

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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];         } } 

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.

回答1:

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

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* 

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) 


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