簡體   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