【问题标题】:Single Linked List that implements push and pop - seg fault实现push和pop的单链表——seg fault
【发布时间】:2014-03-21 05:55:01
【问题描述】:

我正在查看一些旧代码来编写一个程序(用 C 语言),该程序创建类似于单链表堆栈的推送和弹出方法。我目前遇到分段错误,无法解决问题。

任何推送的输入都是单个字符,这是一个输入示例:
推;
按g
推。
流行音乐
推——

代码(注释掉一些导致错误的东西):

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

struct node
{
    char data;
    struct node* next;
}*top = NULL;

void push(char c);
char pop();

int main(int argc, char *argv[])
{
    char* p1;
char p2;
FILE *fp = NULL;
fp = fopen(argv[2], "r");

loop:
    while (!feof(fp)) 
    {
        fscanf(fp,"%s", &p1);

        while (strcmp(&p1,"push") == 0) 
        {
            fscanf(fp,"%s", &p2);
            printf("%s\n", &p2); 
            // push(&p2);
            fscanf(fp,"%s", &p1);
            if (strcmp(&p1,"pop") == 0) 
            {
                //pop();
                 fscanf(fp,"%s", &p1);
            }
        }

        while (strcmp(&p1,"pop") == 0) 
        {
            //pop();??
            fscanf(fp,"%s",&p1);
            if (strcmp(&p1,"push") == 0) 
            {
                fscanf(fp,"%s",&p2);
                printf("%s\n",&p2); 
                // push(&p2);
            }
            goto loop;
         }
    }

    fclose(fp);
    return 0;   
}

void push(char c) 
{
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->data = c;
    temp->next = top;
    top = temp;
}

char pop() 
{
    struct node *temp = top;
    char data = temp->data;
    top = top->next;
    free(temp);
    return data;
}

当前警告:

stack.c: In function âmainâ:

stack.c:24:3: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]

stack.c:26:3: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]

/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â

stack.c:31:4: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]

stack.c:32:4: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]

/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â

stack.c:35:5: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]

stack.c:39:3: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]

/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â

stack.c:42:4: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]

stack.c:43:4: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]

/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â

【问题讨论】:

  • 您对p1p2的使用完全错误,这就是您的分段错误的原因。
  • 我明白了,更改了这些并查看了接受的类型与我提供的类型。感谢您的提示。

标签: c linked-list stack


【解决方案1】:

警告已经告诉你,该怎么做。函数期望的指针 char* 和您传递的指针 &amp;p1 aka char** 是不同的类型。要解决此问题,您必须使用 p1 而不是 &amp;p1

但是你有另一个问题,你在初始化之前使用了p1。这也会导致分段错误,因为指针不指向任何地方,而函数 printfstrcmp 尝试访问那里的内容。

这意味着在fscanf 的情况下,您必须分配可以存储内容的内存,请参阅malloc。当然,p2 也是如此。

【讨论】:

  • 明白了,非常感谢。对不起,我对 C 的菜鸟理解!
【解决方案2】:

首先 p1 没有任何内存可以指向。其次,您将 &p1 传递给 fscanf,但 fscanf 期望它是 char * 并且 &p1 具有 char **,因为您已将其改为 char *p1 和 char *p2。我建议您从头开始编码而不是应对代码因为这段代码有很多 intax 错误。可能你的分段错误是因为 fscanf() 本身因为你传递的是 wring 类型的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-10
    • 2021-01-16
    • 1970-01-01
    • 2011-05-04
    • 2014-08-12
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多