C 为什么有些角色会改变?

C 为什么有些角色会改变?,c,string,C,String,我正在使用GNU GCC编译器在代码块中使用C语言编程。我正在编写一个函数来创建一些链接列表,其中包含令牌作为节点。例如,对于以下文本文件: main ( ) { int a ; int b ; } 令牌的链接列表将是 main->->->{->int->a->;->int->b->;->} 其分隔符是空格字符。 然后我决定制作一些其他的链接列表,称为line。每一行由连续的标记组成,标记之间用空格分隔,标记结束;性格例如,在具有相关标记的同一文本文件中,行为: main{inta;->int

我正在使用GNU GCC编译器在代码块中使用C语言编程。我正在编写一个函数来创建一些链接列表,其中包含令牌作为节点。例如,对于以下文本文件:

main ( )
{
int a ;
int b ;
}
令牌的链接列表将是

main->->->{->int->a->;->int->b->;->}

其分隔符是空格字符。 然后我决定制作一些其他的链接列表,称为line。每一行由连续的标记组成,标记之间用空格分隔,标记结束;性格例如,在具有相关标记的同一文本文件中,行为:

main{inta;->intb;->}

您可以在下面看到我的代码:

//including the related Header files
typedef struct token {
    char *tok;
    struct token *next;
    int tp;
} token;


typedef struct line {
    char *ls;
    struct line *next;
    int parent;
} line;

token *start;
line *lstart;

void addline (line * a);
void showline (void);
void setline (void);

int main (void ) {

    int i = 0;

    // the next 4 lines allocates some space for start(pointer of type token)
    // and lstart(pointer of type line) as the first node in the link
    // list.The first meaningful data of each type are stored in the nodes
    // after the start and lstart node


    start = (token *) malloc (sizeof (token));
    start->next = NULL;

    lstart = (line *) malloc (sizeof (line));
    lstart->next = NULL;

    FILE *p;

    p = fopen ("sample.txt", "r+");
    if (p == NULL) {
        printf ("Can Not Open File");
        exit (1);
    }
    //calling some fnuction for making link list of tokens from the text
    //file

    setline ();
    showline ();

    return 0;

}

// the relevant add functions which adds a new token or 
// link list at the end of the list
void showline ()
{
    line *p;
    p = lstart->next;
    while (p != NULL) {
        printf ("%s\n", p->ls);
        p = p->next;
    }
}

void setline (void)
{
    int parent;
    token *p;
    p = start->next;

    line *q;

    q = (line *) malloc (sizeof (line));

    q->ls = NULL;

    while (p != NULL) {
        if (p == NULL) {
            break;
        }

        q->ls = strdup (p->tok);
        strcat (q->ls, " ");
        p = p->next;

        while ((p != NULL)) {
            if (strcmp (p->tok, ";") == 0) {
                strcat (q->ls, "; ");
                p = p->next;
                break;
            }
            strcat (q->ls, p->tok);
            strcat (q->ls, " ");
            p = p->next;
        }

        printf ("%s\n", q->ls);
        addline (q);
        q->ls = NULL;
    }
}
我在文本文件sample.txt中存储了一些数据:

#include <something.h> 
int a , b ;
main ( )
{
int a ;
int b ;
}
我希望可以正确地生成行,但调用showline函数时出现了一些奇怪的情况。使用此函数时,可以在主视图中看到:在某些行中有一些奇怪的字符。例如,第二行节点的ls预期为int b;但真正发生的是înt b;通常的i字符变成了一个奇怪的字符。我在处理字符串时犯了什么错误

其中一个有问题的地方:

q->ls=strdup(p->tok);
strcat(q->ls," ");
strdup函数只为p->tok分配足够的空间,在copies字符串之后没有空间追加任何内容。所以调用strcat当然会写越界,并且您将有未定义的行为


如果您想添加更多字符,需要使用malloc或calloc分配您自己所需的大小,然后手动复制初始字符串。

是否可以在代码段中添加addline定义。q=line*mallocsizeofline;如果您将C编译器用作malloc return void*,则此处不需要类型转换,这将使我自动进行类型转换。@D.7:谢谢,但这不是我的主要问题。例如,如果我使用以下命令:q->ls=char*malloc100*sizeofchar;知道每行少于100个字符,问题就解决了?我为q->ls分配了空间。我将strdup行改为:strcpyq->ls,p->tok。但我得到了运行时错误!