简体   繁体   English

这个问题“取消引用指向不完整类型‘结构收银员’的指针”是什么问题?

[英]What is this problem "dereferencing pointer to incomplete type 'struct cashier' "?

I am learning data structure of queue, and making a cashier structure like this: 1 There are 2 integers,1 float and 1 queue data type in it.我正在学习队列的数据结构,并制作这样的收银员结构: 1其中有 2 个整数、1 个浮点数和 1 个队列数据类型。 2 So I wanted to make a cashier pointer to point to the cashier structure.` 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;
};

But then I get the error:但后来我得到了错误:

/cygdrive/c/Users/Heta/Desktop/CLionHWQ2/supermarket.c:8:6: error: dereferencing pointer to incomplete type 'struct cashier'
     y->numberOfCusCustomersServed=0;

And below is basically the queue function.下面基本上是队列功能。 3 The main() is not yet finished, mostly just empty. 3 main() 还没有完成,大部分都是空的。 4 Any help from this will be appreciated. 4对此的任何帮助将不胜感激。 :) :)

1. 1.

struct cashier *y;

y is a pointer to struct cashier , but it was not set to point to a variable of type struct cashier . y是一个指向struct cashier的指针,但它没有被设置为指向一个struct cashier类型的变量。

Thus,因此,

    y->numberOfCusCustomersServed=0;
    y->totalCustomerWaitingTime=0;
    y->totalAmountReceived=0.0;
    y->customerQ=getEmptyQueue();

    return y;      // discussed further at point 2.

doesn´t work, since y has not been initialized to point to a valid variable of type struct cashier .不起作用,因为y尚未初始化为指向struct cashier类型的有效变量。

Rather use:而是使用:

    struct cashier x;                  // x is a variable of type `struct cashier`.
    struct cashier *y = &x;            // y is a pointer to x.

    y->numberOfCusCustomersServed = 0;
    y->totalCustomerWaitingTime = 0;
    y->totalAmountReceived = 0.0;
    y->customerQ = getEmptyQueue();

with x as a variable of type struct cashier and y as pointer to it, or alternatively:x作为struct cashier类型的变量,将y作为指向它的指针,或者:

    struct cashier x;

    x.numberOfCusCustomersServed = 0;
    x.totalCustomerWaitingTime = 0;
    x.totalAmountReceived = 0.0;
    x.customerQ = getEmptyQueue();

since the pointer of y is redundant.因为y的指针是多余的。


2. 2.

return y;

You cannot return a pointer to an inner-scope variable from a function, because the variable the pointer points to does no longer exist when the function is finished.您不能从函数返回指向内部作用域变量的指针,因为当函数完成时,指针指向的变量不再存在。

If you really want to return a structure, you should do it like that:如果你真的想返回一个结构,你应该这样做:

struct cashier initCashier(void){
    
    struct cashier x;                  // x is a variable of type `struct cashier`.
    struct cashier *y = &x;            // y is a pointer to x.

    y->numberOfCusCustomersServed = 0;
    y->totalCustomerWaitingTime = 0;
    y->totalAmountReceived = 0.0;
    y->customerQ = getEmptyQueue();

    return x;                           // returning the complete structure.
};

or alternatively, since you do not need to use y , a pointer to the structure, otherwise:或者,由于您不需要使用y ,指向结构的指针,否则:

struct cashier initCashier(void){
    
    struct cashier x;                  // only the structure variable x. 

    x.numberOfCusCustomersServed = 0;
    x.totalCustomerWaitingTime = 0;
    x.totalAmountReceived = 0.0;
    x.customerQ = getEmptyQueue();

    return x;                           // returning the complete structure.
};

Be sure to also change the respective type of the return value of the function initCashier() from struct cashier* to struct cashier .一定还要将函数initCashier()的返回值的相应类型从struct cashier*更改为struct cashier


3. 3.

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;
 _______

The second cashier defines a global variable of type struct cashier with the identifier of cashier , which is permissible because structure tag and global variables are in different name spaces, but you do not use this variable in the provided code, so either you can omit it (if you do not need it):第二个cashier定义了一个struct cashier类型的全局变量,标识符为cashier ,这是允许的,因为结构标签和全局变量在不同的命名空间中,但是你在提供的代码中没有使用这个变量,所以你也可以省略它(如果你不需要它):

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 */
};

or you can use this one, which would solve half of the problems mentioned above, if you only want to use the global variable cashier of type struct cashier for the function initCashier() :或者你可以使用这个,它可以解决上面提到的一半问题,如果你只想对函数initCashier()使用struct cashier类型的全局变量cashier

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;                     // global variable `cashier` of type `struct cashier`.

void initCashier(void){                        // return type of `void`.
    cashier.numberOfCusCustomersServed = 0;    // the variable `cashier` is used directly.
    cashier.totalCustomerWaitingTime = 0;
    cashier.totalAmountReceived = 0.0;
    cashier.customerQ = getEmptyQueue();
};

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

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