将指向结构数组的指针传递给函数的区别

Difference in passing a pointer to an array of structures to a function

是否可以将指向结构数组的指针传递给函数?当我尝试这种语法时,出现错误。但是,如果我从函数原型中删除 * 并将 & 放在传递结构的位置,我不会收到错误,这是为什么呢?

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) 

例如&Data 不是 Customer *[]Customer *[] 类型是指向 Customer.

的指针数组

&Data的类型是Customer (*)[AMT_OF_CUSOTMERS]。 IE。它是指向 AMT_OF_CUSTOMERS 结构数组的指针。

这两种类型非常不同。


将数组传递给函数的通常方法是让数组衰减到指向其第一个元素的指针。

那么你会

void Get_Customer_Data(Customer *, int);      // Function prototype

并称其为

Get_Customer_Data(Data, AMT_OF_CUSTOMERS);

这样使用Data时,与传递&Data[0]是一样的。