简体   繁体   English

将“ for”与指针用作循环变量时的无限循环

[英]Infinite Loop when using 'for' with pointer as loop variable

I am quite new to coding and got stuck with a problem. 我对编码很陌生,遇到了问题。 I tried to solve it myself and have been googling a lot, but I still do not have a solution to this. 我尝试自己解决该问题,并进行了很多次谷歌搜索,但是我仍然没有解决方案。 Maybe one of you can help? 也许你们其中之一可以帮助您?

This is my code: 这是我的代码:

int main(int argc, char **argv) {
    struct node {
        char *str;
        int count;
        struct node *next;
    };

    struct node head = { argv[1], 1, NULL };
    for (int i = 2; i < (argc); i++) {
        for (struct node *p = &head; (p != NULL); p = p->next) {
            printf("%s,%s\n", argv[i], p->str);
            if (strcmp(argv[i], p->str) == 0) {
                printf("case1\n");
                p->count++;
                break;
            }
            else if ((strcmp(argv[i], p->str) != 0) && p->next) {
                printf("case2\n");
                printf("Adresse, auf die p zeigt: %p", &p);
                continue;
            }
            else if ((strcmp(argv[i], p->str) != 0) && (!p->next)) {
                printf("case3\n");
                struct node *oldhead = &head;
                head.str = argv[i];
                head.count = 1;
                head.next = oldhead;
                break;
            }

        }
    }

    // Print how many times each string appears

    return 0;
}

The goal is to create a linked list that contains all the arguments I gave to the main() when calling the program. 目的是创建一个链接列表,其中包含我在调用程序时提供给main()所有参数。 If there is a duplicate, the structure should count them. 如果有重复项,则结构应将其计数。 For example, if i call the program like ./a.out foo fool foo the result should be a list of length two, where the first element contains the string "foo" and count 2 , and the second element contains the string "fool" and has a count of 1 . 例如,如果我像./a.out foo fool foo这样调用程序,则结果应为长度为2的列表,其中第一个元素包含字符串"foo" ,计数为2 ,第二个元素包含字符串"fool"并且计数为1 The problem is the else if -statement within the inner for loop. 问题是内部for循环中的else if -statement。 This is the only part where the inner for loop should actually be used and assign p->next to p . 这是其中内for循环应该实际使用,并为其分配的唯一部分p->nextp Unfortunately that is not happening. 不幸的是,这没有发生。 The result is that the inner for loop starts over and over again and the pointer p points to the same address all the time (I used printf to figure that out). 结果是内部的for循环一遍又一遍地开始,并且指针p一直一直指向相同的地址(我用printf找出了)。

Does any of you have an idea what could be the problem here? 你们中的任何一个有一个想法,这可能是什么问题吗? I tried everything I could and tried to find a solution online ... 我尽了一切努力,并尝试在网上找到解决方案...

Thanks a lot!!! 非常感谢!!!

The issue is in this part of the code 问题出在代码的这一部分

   else if ((strcmp(argv[i], p->str) != 0) && (!p->next)) {
        printf("case3\n");
        struct node *oldhead = &head;
        head.str = argv[i];
        head.count = 1;
        head.next = oldhead;
        break;
    }

You need to allocate a new struct and then put its address in the last struct entry. 您需要分配一个新的结构,然后将其地址放在最后一个结构条目中。

       else if ((strcmp(argv[i], p->str) != 0) && (!p->next)) {
            printf("case3\n");
            struct node *oldhead = p;
            p = (struct node *) malloc(sizeof(node));
            if (p == NULL) { .... manage the error ... }
            oldhead->next = p;
            p->str = argv[i];
            p->count = 1;
            p->next = NULL;
            break;
        }

Now you're creating nodes and stringing them together. 现在,您正在创建节点并将它们串在一起。 You were effectively updating the same node before. 您之前实际上是在更新同一node

        struct node *oldhead = &head;
        head.str = argv[i];
        head.count = 1;
        head.next = oldhead;

That's not creating a new node. 那不是在创建一个新节点。 It's just creating a new reference to the same node, therefore causing an infinite loop when you try to read the linked list until the end. 它只是创建对同一节点的新引用,因此当您尝试读取链接列表直到最后时会导致无限循环。 Therefore, your program only ever has one node. 因此,您的程序只有一个节点。 You need to actually allocate and create new ones. 您实际上需要分配并创建新的。

The main problem here is 这里的主要问题是

struct node *oldhead = &head;

which you should do malloc : 你应该做malloc

struct node *oldhead = (struct node*) malloc(sizeof(struct node));

so you really allocate a piece of memory for your new node. 因此您实际上为新节点分配了一块内存。 And because you have malloc , you should do free at the end of your program as well: 并且由于您具有malloc ,因此您也应该在程序结束时进行free

while(...) {
   free(deepest_node)
}

the way you do the loop above is to go from the farthest node in the linked list all the way back to the head . 进行上述循环的方法是从链接列表中最远的节点一直返回到head

The other issue is that, you should not append your new node to head : 另一个问题是,您不应将新节点附加到head

head.next = oldhead;

but should be to p , which is the last node in your linked list: 但应该是p ,它是链接列表中的最后一个节点:

p -> next = oldhead;

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

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