简体   繁体   English

在这种情况下如何正确使用双指针?

[英]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.我正在做一个登机队列,我正在使用一个简单的冒泡排序算法对我拥有的队列进行排序,我使用 2 个指针来存储我正在比较的值,并使用 2 个双指针来修改我想要交换的每个节点内的值. 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) ,这个: **r = &(r->next) ,

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

or this: **r = &(**r->next)或者这个: **r = &(**r->next)

Same happens when I try to change where my double pointer "a" is pointing.当我尝试更改双指针“a”指向的位置时也会发生同样的情况。 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如果像这样声明和初始化指针r

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

then in this statement那么在这个声明中

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

**r has the type Passenger . **r具有类型Passenger Moreover r is pointing to a pointer.此外 r 指向一个指针。 S this expression r->next is incorrect because pointers do not have the data member next (pointers are not structures). S 这个表达式r->next是不正确的,因为指针没有数据成员 next(指针不是结构)。

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 .也是无效的,因为结构BoardingQueue没有数据成员next

also the condition in the for loop也是for循环中的条件

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.:)仅在size小于或等于 1 的情况下才有效。:)

You have to rewrite the function sortBoardingQueue anew at least using valid C expressions.您必须至少使用有效的 C 表达式重新重写 function sortBoardingQueue After that you can ask a question: why does not my sort function work.:)之后你可以问一个问题:为什么我的排序 function 不起作用。:)

-- 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:我重组了您的示例代码 + 添加了一个主要的 function 以显示一些编译器错误以显示我的意思:


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); 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 .从这一点来看,它是因为boardingQueue 没有next成员,只有headtail (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.对于这个, r 的类型是Passenger ** ,所以它指向一个乘客结构。 If you really want to access that struct's next field, you'll have to use (*r)->next如果您真的想访问该结构的下一个字段,则必须使用(*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.对于这个, a是单个指针。 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.所以,我希望这对你有很多帮助,下次,如果你发布一个最小的、可重复的例子https://stackoverflow.com/help/minimal-reproducible-example你一定会更快地得到更好的答案。

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

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