简体   繁体   中英

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. 2 So I wanted to make a cashier pointer to point to the cashier structure.`

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. 4 Any help from this will be appreciated. :)

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 .

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 .

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:

    struct cashier x;

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

since the pointer of y is redundant.


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:

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 .


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):

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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