C语言链表节点创建异常及段错误排查求助
First, let's tackle each of your problems step by step:
Problem 1: Unexpected Node Numbers (Output Shows 1 3 3)
The root cause here is that you're only allocating memory for one node outside the loop in creation_noeud, then reusing that same pointer for every iteration. This means all "nodes" in your linked list are actually the same block of memory—each loop overwrites the num and u values, and the next pointers end up creating a circular reference (pointing back to the same node). That's why you see repeated 3s.
Fix for Node Numbering
Move the malloc call inside the loop so you create a brand new node each time:
void creation_noeud(maillages mail){ int i; noeuds new; mail->lst_noe = NULL; for (i = 0; i < mail->nb_noe; i++){ // Allocate a new node for each iteration new = (noeuds)malloc(sizeof(struct noeud)); if (!new) { // Always check if malloc succeeded! perror("Failed to allocate node"); exit(EXIT_FAILURE); } new->num = i + 1; printf("Deplacement du noeud %d: ", new->num); scanf("%lf", &new->u); new->next = mail->lst_noe; mail->lst_noe = new; } }
With this fix, your linked list will use the head-insert method correctly—so the first node in the list will be the last one you created (number 3), followed by 2, then 1. Your printf will now output 1 3 2 for the first three parameters.
Problem 2: Segmentation Fault
There are a few issues causing the segfault—let's fix them one by one:
Issue 1: Mismatched printf Format and Arguments
In your main function, you have 3 %d placeholders but pass 4 integer arguments:
// Wrong: 3 format specifiers, 4 arguments printf("aaa %d %d %d aaa \n",1,mail->lst_noe->num,mail->lst_noe->next->num,mail->lst_noe->next->next->num);
This triggers undefined behavior, which can lead to a segfault. Update the format string to match the number of arguments:
printf("aaa %d %d %d %d aaa \n",1,mail->lst_noe->num,mail->lst_noe->next->num,mail->lst_noe->next->next->num);
Issue 2: recherche_noe Corrupts the Original List
Right now, your recherche_noe function modifies the original list's head pointer (mail->lst_noe) when traversing. This destroys the list structure, leading to broken pointers and segfaults later. Instead, use a temporary pointer to traverse without altering the original list:
noeuds recherche_noe(maillages mail,int num){ noeuds temp = mail->lst_noe; // Use a temp pointer to traverse printf("%d %d ",num, mail->lst_noe->num); while(temp != NULL && temp->num != num){ temp = temp->next; } return temp; // Returns NULL if not found, the node if found }
Issue 3: Unchecked Null Pointers
If you try to access next on a NULL pointer (e.g., if you create fewer than 3 nodes but access lst_noe->next->next), you'll get a segfault. Always add checks before dereferencing pointers:
// In main, check if the list has enough nodes before accessing if (mail->lst_noe && mail->lst_noe->next && mail->lst_noe->next->next) { printf("aaa %d %d %d %d aaa \n",1,mail->lst_noe->num,mail->lst_noe->next->num,mail->lst_noe->next->next->num); } else { printf("Not enough nodes in the list!\n"); }
Bonus: Avoid Memory Leaks
To clean up allocated memory when your program finishes, add a helper function to free the linked list:
void free_noeuds(noeuds head) { noeuds temp; while (head != NULL) { temp = head; head = head->next; free(temp); } } // Call this in main before exiting free_noeuds(mail->lst_noe); free(mail);
内容的提问来源于stack exchange,提问作者Talal C.

