質問

I don't really understand the error:

:58: error: incompatible type for argument 1 of ‘sumData’

:14: note: expected ‘double *’ but argument is of type ‘double’

Here's the code that the error reports refer to:

:14: double sumData(double data[],int size);

int main(){
     . . . 

     int size;
     double sData;

     . . .

     double data[size];

     . . . .

:58: sData=sumData(data[size],size);

     . . . 

return 0;
}
役に立ちましたか?

解決

Assuming this is In C++ (or C?). Arrays are passed by reference. The array name is a pointer to the first memory location. In your case, you need to pass data instead of data[size] as the latter ends up passing the value at data[size] (which should go out of bounds since your array length is size but arrays are accessed by index (so technically you would go up to size-1).

Here's what you need to do to fix it:

sData=sumData(data,size); // when you pass data, you're passing the address of the first memory location that data points to.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top