任何人都可以帮助我解决这个分段错误。这段代码很简单,但错误很难弄清楚。
struct Link {
int key;
unsigned data: 2;
struct Link *next;
struct Link *previous;
};
struct Link* addInOrder(struct Link *, struct Link);
int main() {
struct Link *head;
struct Link data1;
struct Link data2;
struct Link data3;
data1.key = 25;
data1.data = 1;
data1.next = NULL;
data2.key = 50;
data2.data = 0;
data2.next = NULL;
data3.key = 100;
data3.data = 2;
data3.next = NULL;
head = NULL;
head = addInOrder(head, data2);
}
struct Link* addInOrder(struct Link *srt, struct Link l) {
if(!srt) {
return &l;
}
struct Link *temp = srt;
while(temp->next && l.key > temp->key)
temp = temp->next;
printf("here\n");
if(l.key > temp->key) {
printf(" 1\n");
temp->next = &l;
l.previous = temp;
}
else {
printf(" 2\n");
l.previous = temp->previous;
l.next = temp;
printf( "2.2\n");
if(temp->previous) {
//printf("%i\n",temp->previous->key);
temp->previous->next = &l;
}
printf(" 2.3\n");
temp->previous = &l;
}
return srt;
}
我在 addInOrder() 的第一行不断收到错误消息。编译器所说的只是分段错误。
编辑:另外,如果我添加printf("..."); 在 if 语句之后并运行它...不打印