0

我正在参加 C 考试,在尝试将元素插入链表时,遇到了运行时问题。我唯一的目的是将 4 个元素添加到列表中,然后打印列表。但是,它给出了一个错误。我已经看过一些插入代码,我的代码似乎是正确的。看不到错误。任何援助将不胜感激。

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

struct ders{
    char kod;
    struct ders *next;

}*header;
typedef struct ders Ders;
void add(Ders*,Ders*);
void print(Ders*);

int main(void)
{

header = NULL;
Ders *node = NULL;
int i = 0;
char c;
while(i<4)
{
    scanf("%c",&c);
    node = (Ders*)malloc(sizeof(Ders));
    node->kod = c;
    node->next = NULL;
    add(header,node );
    i++;


}
print(header);

return 0;
}

void add(Ders *header, Ders *node)
{
    if(header == NULL){
        header = node;
        header->next = NULL; }
    else{
        node->next = header;
        header = node;

    }
}

void print(Ders *header)
{
Ders *gecici = header;

while(gecici != NULL){
    printf("%c\n",gecici->kod);
    gecici = gecici->next;
}
}
4

1 回答 1

1

正如 nihirus 所说,“指针是按值传递的。因此,您可以更改它指向的内存,但不能更改实际指针,即使其指向其他东西。”

您的修改导致错误*header is not member of struct ,因为 -> 优先级高于 *

尝试 (*header)->next = NULL 改用。

C 运算符优先级: http ://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

于 2013-03-03T16:43:44.887 回答