键盘输入使函数无法正常工作-C

键盘输入使函数无法正常工作-C,c,c-standard-library,C,C Standard Library,我有以下代码: browser.h #include <stdlib.h> #include <string.h> typedef struct webpage { struct webpage *prev; struct webpage *next; char* url; } Webpage; Webpage *current = NULL; void free_mem(Webpage *p) { if (p->next !=

我有以下代码:

browser.h

#include <stdlib.h>
#include <string.h>

typedef struct webpage {
    struct webpage *prev;
    struct webpage *next;
    char* url;
} Webpage;

Webpage *current = NULL;

void free_mem(Webpage *p) {
    if (p->next != NULL) {
        free_mem(p->next);
    }
    free(p);
}

char * add_url(char *url) {
    Webpage *p = malloc(sizeof(Webpage));
    p->url = url;
    if (current != NULL) {
        if (current->next != NULL) {
            free_mem(current->next);
        }
        current->next = p;
        p->prev = current;

    } else {
        p->prev = NULL;
    }
    p->next = NULL;
    current = p;
}

void list_history() {
    Webpage *temp = current;
    printf("--- HISTORIAL ---\n");
    while(temp != NULL) {
        printf("%s\n", temp->url);
        temp = temp->prev;
    }
    printf("--- FIN ---\n");
}

void nav_back() {
    if (current->prev != NULL) {
        current = current->prev;
    } else {
        printf("IGNORADO\n");
    }
}

void nav_forward() {
    if (current->next != NULL) {
        current = current->next;
    } else {
        printf("IGNORADO\n");
    }
}
将评论复制到答案中


创建一个函数来打印给定起点的列表。使用它来查看列表发生了什么

您一直将相同的缓冲区传递给循环中的
add_url()
,但它包含最近编写的内容。工作代码每次在不同的存储中传递不同的字符串。复制字符串,然后将其保存在列表-查找中。

将注释复制到答案中


创建一个函数来打印给定起点的列表。使用它来查看列表发生了什么


您一直将相同的缓冲区传递给循环中的
add_url()
,但它包含最近编写的内容。工作代码每次在不同的存储中传递不同的字符串。复制字符串,然后将其保存到列表-查找中。

请显示一个运行示例,并解释输出的错误。当您逐步使用调试器时,会发生什么情况,调试器是否达到预期效果?@pm100我尝试在代码块中使用调试器,但过了一段时间后它崩溃创建一个函数以给定起点打印列表。使用它来查看列表发生了什么。您一直将相同的缓冲区传递给循环中的
add_url()
,但它包含最近编写的内容。工作代码每次在不同的存储中传递不同的字符串。复制字符串,然后将其保存到列表查找中。@JonathanLeffler请将您的评论作为答案发布。它解决了问题。请显示一个运行示例并解释输出的错误。当您逐步使用调试器时,会发生什么情况,调试器是否达到了预期效果?@pm100我尝试在代码块中使用调试器,但一段时间后它崩溃创建一个函数以打印给定起点的列表。使用它来查看列表发生了什么。您一直将相同的缓冲区传递给循环中的
add_url()
,但它包含最近编写的内容。工作代码每次在不同的存储中传递不同的字符串。复制字符串,然后将其保存到列表查找中。@JonathanLeffler请将您的评论作为答案发布。它解决了这个问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "browser.h"

int main() {
    add_url("www.google.com");
    add_url("www.something.com");
    add_url("www.a.com");
    add_url("www.b.com");
    add_url("www.c.com");
    add_url("www.d.com");
    add_url("www.e.com");
    list_history();
    nav_back();
    nav_back();
    list_history();
    nav_forward();
    list_history();
    add_url("www.new-url.com");
    list_history();
    system("pause"); // this line only works on Windows
    return 0;
}

//int main (int argc, char *argv[]) {
//    add_url(argv[1]);
//    char *option = "Z";
//    char input_text[2002];
//    while (1) {
//        printf("Ingrese comando(A,S,U,L,Q) (URL actual = %s): ", current->url);
//        fgets(input_text, 2002, stdin);
//        option = strtok(input_text, "\n ");
//        if (strcmp(option, "A") == 0) {
//            nav_back();
//        } else if (strcmp(option, "S") == 0) {
//            nav_forward();
//        } else if (strcmp(option, "U") == 0) {
//            char *url = strtok(NULL, "\n");
//            printf("La URL es: %s\n", url);
//            add_url(url);
//        } else if (strcmp(option, "L") == 0) {
//            list_history();
//        } else if (strcmp(option, "Q") == 0) {
//            break;
//        } else {
//            printf("OPCION NO VALIDA\n");
//        }
//    }
//}
Ingrese comando(A,S,U,L,Q) (URL actual = (null)): U a.com
La URL es: a.com
Ingrese comando(A,S,U,L,Q) (URL actual = a.com): U b.com
La URL es: b.com
Ingrese comando(A,S,U,L,Q) (URL actual = b.com): U c.com
La URL es: c.com
Ingrese comando(A,S,U,L,Q) (URL actual = c.com): U d.com
La URL es: d.com
Ingrese comando(A,S,U,L,Q) (URL actual = d.com): U e.com
La URL es: e.com
Ingrese comando(A,S,U,L,Q) (URL actual = e.com): U f.com
La URL es: f.com // works fine up to here
Ingrese comando(A,S,U,L,Q) (URL actual = f.com): L // L should show a list of all URLS written above
--- HISTORIAL ---






--- FIN --- // You can see that history has the space for the URLs but none was printed
Ingrese comando(A,S,U,L,Q) (URL actual = ): A // now it doesn't show current URL "URL actual" any more. Notice before it was showing current after I added one.
Ingrese comando(A,S,U,L,Q) (URL actual = ): Q