Reputation: 167
Is it possible to pass a pointer to an array of structures to a function? When i try this syntax, i get an error. However, if I remove the * from the function prototype and drop the & where I'm passing the structure, I do not receive an error, why is that?
struct Last_Payment_Date // Date Last Payment was made by customer
{
int month;
int day;
int year;
};
struct Residence // Residence of Customer
{
string Address;
string City;
string State;
string ZIP;
};
struct Customer // Customer information
{
string Name;
Residence Place;
string Telephone;
int AcctBalance;
Last_Payment_Date Date;
};
void Get_Customer_Data(Customer *[], int); // Function prototype
void Display_Customer_Data(Customer [], int);
int main()
{
const int AMT_OF_CUSTOMERS = 2; // Amount of customers
Customer Data[AMT_OF_CUSTOMERS];
Get_Customer_Data(&Data, AMT_OF_CUSTOMERS); // ERROR!
return 0;
}
void Get_Customer_Data(Customer *[], int n)
Upvotes: 2
Views: 41
Reputation: 409404
The type of e.g. &Data
is not Customer *[]
. The type Customer *[]
is an array of pointers to Customer
.
The type of &Data
is Customer (*)[AMT_OF_CUSOTMERS]
. I.e. it's a pointer to an array of AMT_OF_CUSTOMERS
structures.
The two types are very different.
The usual way to pass arrays to a function is to let the array decay to a pointer to its first element.
Then you would instead have
void Get_Customer_Data(Customer *, int); // Function prototype
And just call it as
Get_Customer_Data(Data, AMT_OF_CUSTOMERS);
When using Data
in this way, it is the same as passing &Data[0]
.
Upvotes: 3