简体   繁体   中英

How can I use double pointers correctly in this case?

I am doing a boarding queue and I am using a simple bubble sort algorithm to sort the queue I have, I am using 2 pointers to store the values I am comparing and 2 double pointer to modify the value inside each node that I want to swap. My problem is when I want to change where my double pointers are pointing in order to iterate through all the queue, it never compiles, I have tried:

this: **r = &(r->next) ,

this: **r = &(*r->next) ,

or this: **r = &(**r->next)

Same happens when I try to change where my double pointer "a" is pointing. This is the full method just in case you need to see it,

int sortBoardingQueue(BoardingQueue *qPtr){
    int size = calculateSize(qPtr);

    Passenger *t = qPtr->head;
    Passenger *a = qPtr->head->next;
    Passenger **r = &(qPtr->head);
    Passenger **q = &(qPtr->next);
    for (int i = size-1; i <=0; i--)
    {
        while(t != NULL){
            if (t->seatNumber>a->seatNumber)
            {
                **r = *a;
                **q = *t;
            }
            t = t->next;
            a = a->next;
            **r = &(r->next);
            **a = &(a->next);
        }
    }

}

//the declarations of the structs I am using in my header file
typedef struct boardingQueue {
    Passenger* head; // points to the Passenger at the front/head of the queue
    Passenger* tail; // points to the Passenger at the end/tail of the queue
} BoardingQueue;

typedef struct passenger {
char name[30];          // the passenger's name
double passportNumber;  // their passport number
int seatNumber;         // their seat number on the plane
struct passenger* next; // a pointer to the next passenger in the queue after this one
} Passenger;

If the pointer r is declared and initialized like this

Passenger **r = &(qPtr->head);

then in this statement

**r = &(r->next);

**r has the type Passenger . Moreover r is pointing to a pointer. S this expression r->next is incorrect because pointers do not have the data member next (pointers are not structures).

So all three expressions

**r = &(r->next),

**r = &(*r->next),

**r = &(**r->next)

are incorrect. For example the expression

&(**r->next)

is equivalent to

&(** ( r->next ) )

It seems what you mean is the following

r = &( *r )->next;

Take into account that this declaration

Passenger **q = &(qPtr->next);

is also invalid because the structure BoardingQueue does not have the data member next .

also the condition in the for loop

for (int i = size-1; i <=0; i--)

is invalid. It will be valid only in the case when size is less than or equal to 1.:)

You have to rewrite the function sortBoardingQueue anew at least using valid C expressions. After that you can ask a question: why does not my sort function work.:)

-- next time it will be easier to get an answer if the posted code snippet is exactly what you're feeding into the compiler, and if you include your compiler errors exactly as they appear with your question.

I restructured your example code + added a main function to show some of the compiler errors to show what I mean:


Code example

#define NULL 0
//the declarations of the structs I am using in my header file
typedef struct passenger {
char name[30];          // the passenger's name
double passportNumber;  // their passport number
int seatNumber;         // their seat number on the plane
struct passenger* next; // a pointer to the next passenger in the queue after this one
} Passenger;

typedef struct boardingQueue {
    Passenger* head; // points to the Passenger at the front/head of the queue
    Passenger* tail; // points to the Passenger at the end/tail of the queue
} BoardingQueue;

int sortBoardingQueue(BoardingQueue *qPtr){
    int size = 3; // func undefined?? calculateSize(qPtr);

    Passenger *t = qPtr->head;
    Passenger *a = qPtr->head->next;
    Passenger **r = &(qPtr->head);
    Passenger **q = &(qPtr->next);
    for (int i = size-1; i <=0; i--)
    {   
        while(t != NULL){
            if (t->seatNumber>a->seatNumber)
            {
                **r = *a; 
                **q = *t; 
            }
            t = t->next;
            a = a->next;
            **r = &(r->next);
            **a = &(a->next);
        }
    }   

}

int main(){
    // make passengers
    Passenger a = {"p1", 111.0, 1, NULL};
    Passenger b = {"p2", 222.0, 2, NULL};
    Passenger c = {"p2", 222.0, 3, NULL};

    // make boarding queue a->c->b
    a.next = &c; 
    c.next = &b; 
    BoardingQueue q = {&a, &b};

    sortBoardingQueue(&q);
}

Compiler errors

dp.c: In function 'sortBoardingQueue': dp.c:22:27: error: 'BoardingQueue {aka struct boardingQueue}' has no member named 'next' Passenger **q = &(qPtr->next);

From this one, its because boardingQueue has no member next , only head and tail . (Maybe you wanted that to point to a passenger? I can't tell what you intended from context)

dp.c:33:22: error: request for member ‘next’ in something not a structure or union
              `**r = &(r->next);`

For this one, r is of type Passenger ** , so it points to a passenger struct. If you really want to access that struct's next field, you'll have to use (*r)->next

  dp.c:34:13: error: invalid type argument of unary ‘*’ (have
 ‘Passenger {aka struct passenger}’)
              **a = &(a->next);

For this one, a is a single pointer. You can only dereference it once.


So, I hope that helps you plenty for this assignment, And next time, if you post a minimum, reproducible example https://stackoverflow.com/help/minimal-reproducible-example you're bound to get better answers faster.

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