我試圖從命令列處理多個名稱argv[],然后添加或洗掉以“ ”或“-”開頭的名稱。IE bill ted -ted 將添加 bill 和 ted 然后洗掉 ted。我可以向此串列添加名稱沒問題,但我的removeNode()函式呼叫導致分段錯誤。在我的 main() 中,當我轉到要洗掉的行程名稱時,我將其設定char * name為等于 的回傳指標removeNode(),它應該是來自被釋放節點的字串。回傳此指標的正確方法是什么,以便我可以從命令列中洗掉我參考的名稱?我還包括我的插入功能。
int insert(struct node ** head, char * name) {
struct node * newNode = (struct node * ) malloc(sizeof(struct node));
newNode -> data = name;
newNode -> next = NULL;
struct node * current = * head;
if ( * head == NULL) {
* head = newNode;
return 1;
}
while (current -> next != NULL) {
current = current -> next;
}
current -> next = newNode;
return 1;
}
char * removeNode(struct node ** head, char * name) {
struct node * current = * head;
struct node * previous;
if (current == NULL) {
return "error0";
}
if (strcmp(current -> data, name) == 0) {
char * data = current -> data;
* head = current -> next;
free(current);
printf("Removed %s \n", name);
return data;
}
while (current != NULL) {
previous = current;
current = current -> next;
if (strcmp(current -> data, name) == 0) {
char * data = current -> data;
previous -> next = current -> next;
free(current);
printf("Removed %s \n", name);
return data;
}
}
return "error0";
}
int main(int argc, char * argv[]) {
printf("Author : Torin Costales \n");
struct node * head = NULL;
for (int x = 1; x < argc; x ) {
if (argv[x][0] == ' ') {
char * name = malloc((strlen(argv[x] 1) 1));
if (name == NULL) return EXIT_FAILURE;
strcpy(name, argv[x]);
printf("adding %s \n", name);
insert( & head, name);
printf("List: ");
printList( & head);
} else if (argv[x][0] == '-') {
printf("removing %s \n", argv[x] 1);
char * name = removeNode( & head, argv[x] 1);
free(name);
printList( & head);
}
}
}
uj5u.com熱心網友回復:
導致故障的問題是您正在使用
strcpy(name, argv[x]);
你應該在哪里使用
strcpy(name, argv[x] 1);
這導致"error0"在嘗試洗掉時回傳一個由 生成的字串,ted因為串列包含 bill和 ted。
修復和清理:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *data;
struct node *next;
} node;
void printList(struct node *node) { // Don't need a pointer to a pointer.
printf("List:");
for (; node; node = node->next)
printf(" %s", node->data);
printf("\n");
}
// Returns 0 on success.
// Returns -1 and sets errno on error.
int append(struct node **node_pp, char *name) { // Better name
while (*node_pp)
node_pp = &( (*node_pp)->next );
struct node *newNode = malloc(sizeof(struct node));
if (!newNode)
return -1;
newNode->data = name;
newNode->next = NULL;
*node_pp = newNode;
return 0;
}
// Returns NULL if not found.
char *removeNode(struct node **node_pp, const char *name) { // Added `const`
for (; *node_pp; next_p = &( (*node_pp)->next )) {
if (strcmp((*node_pp) -> data, name) == 0) {
struct node * oldNode = *next_p;
*node_pp = oldNode->next;
char *data = oldNode->data;
free(oldNode);
return data;
}
}
return NULL; // NULL is a far better value to return on error.
}
int main(int argc, char * argv[]) {
struct node *head = NULL;
for (int x = 1; x < argc; x ) {
if (argv[x][0] == ' ') {
char *name = strdup(argv[x] 1); // Simpler
if (name == NULL) {
perror("Can't allocate memory"); // Error message is good
exit(EXIT_FAILURE);
}
printf("Appending %s.\n", name);
if (append(&head, name) < 0) {
perror("Can't append node to list");
exit(EXIT_FAILURE);
}
} else if (argv[x][0] == '-') {
const char *name_to_find = argv[x] 1;
printf("Removing %s.\n", name_to_find);
char *name = removeNode(&head, name_to_find);
if (name) { // Do check if it was found!
free(name);
} else {
printf("%s not found\n", name_to_find);
}
}
printList(head);
printf("\n");
}
}
輸出 foo bar -baz -foo:
Appending foo.
List: foo
Appending bar.
List: foo bar
Removing baz.
baz not found
List: foo bar
Removing foo.
List: bar
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/370320.html
上一篇:C語言位運算問題
