繁体   English   中英

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

[英]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] 也就是说,它是指向AMT_OF_CUSTOMERS结构数组的指针。

两种类型非常不同。


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

然后,您将拥有

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

并称之为

Get_Customer_Data(Data, AMT_OF_CUSTOMERS);

以这种方式使用Data时,与传递&Data[0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM