我正在学习队列的数据结构,并制作一个这样的收银员结构:1其中有2个整数,其中1个浮点数和1个队列数据类型。2因此,我想制作一个收银员指针来指向收银员结构。`
struct cashier {
int numberOfCustomersServed; /* This should be initialized to 0 */
int totalCustomerWaitingTime; /* This should be initialized to 0 */
float totalAmountReceived; /* This should be initialized to 0 */
queueADT customerQ; /* This should be initialized to an empty queue */
}cashier;
struct cashier* initCashier(void){
struct cashier *y;
y->numberOfCusCustomersServed=0;
y->totalCustomerWaitingTime=0;
y->totalAmountReceived=0.0;
y->customerQ=getEmptyQueue();
return y;
};
但随后出现错误:
/cygdrive/c/Users/Heta/Desktop/CLionHWQ2/supermarket.c:8:6: error: dereferencing pointer to incomplete type 'struct cashier'
y->numberOfCusCustomersServed=0;
struct cashier *y;
[y
是指向struct cashier
的指针,但未将其设置为指向类型为struct cashier
的变量。]>
因此,
y->numberOfCusCustomersServed=0; y->totalCustomerWaitingTime=0; y->totalAmountReceived=0.0; y->customerQ=getEmptyQueue(); return y;
无效,因为
y
尚未初始化为指向类型为struct cashier
的有效变量。
而是使用:
struct cashier* initCashier(void){
struct cashier x;
struct cashier *y = &x;
y->numberOfCusCustomersServed=0;
y->totalCustomerWaitingTime=0;
y->totalAmountReceived=0.0;
y->customerQ=getEmptyQueue();
return y;
};