这是数据的结构:

    struct variabile {
        char* nome;
        char* contenuto;
        struct variabile* next;
        int tipo;
    };


这是代码:

    struct variabile* pt = malloc (sizeof (struct variabile));
            pt->nome = $1;
            pt->tipo = type;
            pt->contenuto = $2;
            pt->next=NULL;


    if (testaVariabile == NULL)
            testaVariabile=pt;
    else {
            struct variabile* current = testaVariabile;


        while ((current->next)!=NULL){

            if(strcmp(current->nome, pt->nome)==0)
                                    fprintf (stderr, "warning: clash di nome di variabili %s \n", current->nome);
            current=(current->next);
        }
        (current->next)=pt;

        if(strcmp(current->nome, pt->nome)==0)
                fprintf (stderr, "warning: clash di nome di variabili %s \n", current->nome);
    }


这是变量testaVariabile的初始声明。

       struct variabile* testaVariabile = NULL;


当我尝试更新代码的最后一行中的下一个当前值时,出现分段错误错误。
我还验证了代码没有进入while周期。

我刚接触C,所以请解释答案。

谢谢

编辑

其余代码是Flex解​​析器。

编辑

我的一个朋友告诉我使用gdb进行调试。结果是错误来自

       strcmp(current->nome, pt->nome)


我为在错误的位置上找不到问题所付出的时间感到非常抱歉,但这是我第一次使用C和Flex。

有人知道如何摆脱这个问题吗?

好的,最后更新。

选中的答案解决了问题。
但是在这种情况下,词法分析器从文件中读取值的方式也存在错误,因此这是一个两部分的问题。

最佳答案

尝试这个:

struct variabile* pt = malloc (sizeof (struct variabile));
pt->nome = malloc(strlen($1) + 1);
strcpy(pt->nome, $1);
pt->tipo = type;
pt->contenuto = $2; // maybe the same for this too?
pt->next=NULL;

/* rest of your code ... */


虽然只是一个疯狂的猜测

关于c - 在结构内部使用C Struct会导致段错误错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15399892/

10-11 22:57
查看更多