'Cannot convert 'float' to 'float*' for argument '1'
Solution 1
First of all, note that main(void){
is not a valid signature for main
.
It should be:
int main(void){
Then:
float vett[10];
vett[10] = 0;
This is not valid. Array indices start at 0, so index 10 is out of bounds, as it would require an array with size 11.
Also, as your average
function takes as first argument a float array, you'll need to pass it this way:
average(vett,media);
Using:
average(&vett[10],media);
Will pass a pointer to the data located right after the array, so obviously you'll get junk values.
Solution 2
- Don't use
func(void)
in C++, it's some sort of deprecated. Usefunc()
instead. - If you have
float vett[10];
, thenvett[10]
is invalid. You must use onlyvett[0 .. 9]
float average(float array[10], float &average)
actually means
float average(float *array, float &average)
.
(Second form is more common way to declare pointer arguments.)
If you want to call it withvett
as argument, just useaverage(vett, media);
Names of an arrays, when used as pointers, are automatically converted to pointer to first element of an array. So hereaverage(vett, media);
vett
means&vett[0]
.
Solution 3
The number in the square brackets has two meanings:
- In a declaration like
float vett[10];
it is the size of the array - When not in a declaration, like
average(&vett[10],media);
it means the eleventh element of the array.
average(&vett[10],media);
is passing the address of the eleventh element to the function. The function interprets it as the beginning of the array, which is wrong and causes undefined behaviour when the elements outside of the array are accessed.
Because you want to pass the whole array, you should use
average(vett,media);
Geeh
Updated on April 25, 2020Comments
-
Geeh about 4 years
I'm an ICT student, studying C++, and I find it very interesting. However, while experimenting, I came up to something I can't fix myself nor find on the net. This is the code:
#include <iostream> using namespace std; float average(float array[10], float &average){ int i = 0; for( i = 0; i != 10; i++ ){ cout << "Vett ("<<i<<") = "; cin >> array[i]; while(cin.fail()) { cin.clear(); cin.ignore(); system("Color 5C"); cout << "\nPlease insert a number: "; cin >> array[i]; } average = average + array[i]; } average = average / 10; return array[10]; return average; } main(void){ float vett[10], media; int i; char loop; vett[10] = 0; media = 0; do{ system("cls"); system("Color 00"); cout<<"****************************************"<<endl; cout<<"*** INSER THE DATA TO COMPUTE ***"<<endl; cout<<"****************************************\n"<<endl; /* for( i = 0; i != 10; i++ ){ cout << "Vett ("<<i<<") = "; cin >> vett[i]; while(cin.fail()) { cin.clear(); cin.ignore(); system("Color 5C"); cout << "\nPlease insert a number: "; cin >> vett[i]; } media = media + vett[i]; } media = media / 10; */ average(vett[10],media); for( i = 0; i != 10; i++ ){ cout << vett[i]<<" "; } if(media == 0){ cout << "\nATTENTION the average equals to = "<<media<<endl; } else{ cout << "\nThe average is"<<media<<endl; } printf("\n"); cout << "\nDo You want to continue? Y/N"; cin >> loop; } while(loop == 'Y' || loop == 'y'); system("pause"); }
For some reason I couldn't set in the 'average' function the array as a pointer (&array), perhaps because the array is already a pointer. Nonetheless, removing it gives me the following error:
"Cannot convert 'float' to 'float*' for argument '1' to 'float average(float*,float&)'
If I call the function this way
average(&vett[10],media);
it works, but returns weird values in the array. As you can see, I commented the same thing I put in the function, which works perfectly, unless..I put it in a function. I assume I've done something wrong with the function call, can anybody help me understand?
-
typ1232 about 9 years
vett[10] = 0;
This accesses memory outside of the array bounds and doesn't do what you want. Do you want to zero initialize the array? Usefloat vett[10] = {0}
in the declaration. -
crashmstr about 9 yearsAnd I just saw the same kind of question a few days ago.
-
PaulMcKenzie about 9 years
return array[10]; return average;
What are you expecting to accomplish by usingreturn
twice?
-
-
noelicus about 9 yearsNo. 10 means the 11th element in the array.
-
noelicus about 9 yearsNope. That was a pretty good reason! Have removed it after your edit.
-
alain about 9 yearsThanks, and I fully agree. I'm not so good at playing "The fastest gun in the west"...