Reputation: 13
Please guide me on this code, I want to store list of 5 data using array and function, this is a piece of code of mine, but this is giving me an error ("33"):
Cannot convert `ABC (*)[5]' to `ABC*' for argument `1' to `void pass(ABC*)'
Code:
#include <iostream>
using namespace std;
struct ABC{
char name[20];
int phone;
char address[20];
};
void pass(ABC *abc){
for(int i=0; i<5;i++){
cout<<"Enter name"<<endl;
cin>>abc[i].name;
cout<<"Enter phone"<<endl;
cin>>abc[i].phone;
cout<<"Enter address"<<endl;
cin>>abc[i].address;
}
}
int main()
{
ABC abc[5];
pass(&abc);
system("PAUSE");
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 79
Reputation: 6505
You can use pass(&abc[0]);
or pass(abc);
to get a pointer to the first element in the array. Otherwise if you use &abc
alone you get a pointer to a whole array[5] not the elements inside the array.
Upvotes: 5
Reputation: 704
Arrays are not pointers. But they can decay to pointers when you are doing function calls. So you can pass your array like this:
pass(abc);
Upvotes: 3