I know even if i pass an array by typing arrayname as argument (ex: getArrayInput(arrayexample); ), it will copy only the adress value of first element not entire array,still i wonder why these code gives error. I know this not the way how it should implemented but i want to understand this error.
main.cpp|13|error: cannot convert 'int*' to 'int**' for argument '1' to 'void getArrayInput(int**)'|
#include <iostream>
using namespace std;
void getArrayInput(int * []);
int main()
{
cout<<"Enter scores on by one.." << endl;
cout<<"To terminate input enter -1"<<endl;
int listof[10]={};
int *ptScores =listof;
getArrayInput(ptScores);
return 0;
}
void getArrayInput(int * []){
for(int i=0;i<10;i++){
cin>>*(pt+i);
if(*(pt+i))=-1){
break;
}
else{
cout<<"Enter next.."<<endl;
}
}
}
It is because
int *
and
int[]
are both of type
int *
therefore, you are here asking for a
int **.
try replacing
void getArrayInput(int * []) by void getArrayInput(int *)
In C, arrays decay in to pointers. In some cases, they are interchangable.
ptScores
is of type int*
(pointer to int). getArrayInput
expects an int*[]
(array of pointers to int). int*[]
decays in to int**
(pointer to pointer to int).
The error says you're giving an int*
(ptScores) to something that expects an int**
(getArrayInput).
How do you fix this? Take an int*
.
void getArrayInput(int* pt){
for(int i=0;i<10;i++){
cin>>pt[i];
if(pt[i]=-1){
break;
}
else{
cout<<"Enter next.."<<endl;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.