繁体   English   中英

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

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

我正在学习队列的数据结构,并制作这样的收银员结构: 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;

下面基本上是队列功能。 3 main() 还没有完成,大部分都是空的。 4对此的任何帮助将不胜感激。 :)

1.

struct cashier *y;

y是一个指向struct cashier的指针,但它没有被设置为指向一个struct cashier类型的变量。

因此,

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

    return y;      // discussed further at point 2.

不起作用,因为y尚未初始化为指向struct cashier类型的有效变量。

而是使用:

    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();

x作为struct cashier类型的变量,将y作为指向它的指针,或者:

    struct cashier x;

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

因为y的指针是多余的。


2.

return y;

您不能从函数返回指向内部作用域变量的指针,因为当函数完成时,指针指向的变量不再存在。

如果你真的想返回一个结构,你应该这样做:

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.
};

或者,由于您不需要使用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.
};

一定还要将函数initCashier()的返回值的相应类型从struct cashier*更改为struct cashier


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

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

或者你可以使用这个,它可以解决上面提到的一半问题,如果你只想对函数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