gpt4 book ai didi

c - 段错误 : 11 - C Linked-List

转载 作者:太空宇宙 更新时间:2023-11-04 00:30:41 25 4
gpt4 key购买 nike

我正在尝试在 C 中实现链表。我使用了一个名为 NODE 的结构。出于某种原因,我在调用函数 createList() 时收到了 Segmentation Error 11。我调试了我的代码,我非常确信错误发生在这里:

/* Write into node */
strcpy(head->username, cur_username);
strcpy(head->password, cur_pw);
strcpy(head->type, cur_type);
printf("%s %s %s\n", head->username, head->password, head->type);

我认为错误是因为没有正确分配内存。但是我为每个节点值初始化了一个 50 个字符的数组,所以程序不应该自己管理内存吗?

    char *filename = "password.csv";
int n_users = 0;

struct NODE {
char username[50];
char password[50];
char type[50];
struct NODE *next;
} *head=NULL, *curr=NULL;

/* Global placeholders so they can be used by other both add() and creatList() */
char line[50], *cur_username, *cur_pw, *cur_type;
char linecopy[50];

void createList(void) {
printf("Creating list\n");

bool success;

/* Open the file in read-only and copy content into array */

/* File pointer */
FILE *f;

/* Node pointer */
struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE));
if (NULL == ptr) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}

head = curr = ptr;

/* Open the file in read */
f = fopen("password.csv", "r");

/* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */
if (f != NULL) {
/* Read the file line by line */
while(fgets(line, 49, f) != NULL) {

struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE));

/* Copy line into linecopy because strtok is destructive */
strcpy(linecopy, line);

/* Extract the ids from linecopy */
cur_username = strtok(linecopy, ",");;
cur_pw = strtok(NULL, ",");
cur_type = strtok(NULL, ",");

if(head==NULL)
add(head);
else
add(p);

if (!success) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
}
}

/* Close file */
fclose(f);
}

** 头为空 **

虽然我不再有任何段错误,但我的头没有得到初始化...我正在从文件中读取输入并为文件的每一行创建一个节点。因此,我正在测试 head==NULL 并将 head 发送到 add(struct *NODE)。不应该初始化 head 吗?

struct NODE {
char username[50];
char password[50];
char type[50];
struct NODE *next;
} *head=NULL, *curr=NULL;

/* Global placeholders so they can be used by other both add() and creatList() */
char line[50], *cur_username, *cur_pw, *cur_type;
char linecopy[50];

void createList(void) {
printf("Creating list\n");

bool success;

/* Open the file in read-only and copy content into array */

/* File pointer */
FILE *f;

/* Node pointer */
struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE));
if (NULL == ptr) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}

head = curr = ptr;

/* Open the file in read */
f = fopen("password.csv", "r");

/* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */
if (f != NULL) {
/* Read the file line by line */
while(fgets(line, 49, f) != NULL) {

struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE));

/* Copy line into linecopy because strtok is destructive */
strcpy(linecopy, line);

/* Extract the ids from linecopy */
cur_username = strtok(linecopy, ",");;
cur_pw = strtok(NULL, ",");
cur_type = strtok(NULL, ",");

if (head == NULL)
success = add(head);
else
success = add(p);

if (!success) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
}
}

/* Close file */
fclose(f);
}

最佳答案

是的,这会是个问题。

  /* Is list empty? */
if (head == NULL) {

/* Write into node */
strcpy(head->username, cur_username);
strcpy(head->password, cur_pw);
strcpy(head->type, cur_type);
printf("%s %s %s\n", head->username, head->password, head->type);

}

取消引用 NULL 通常会导致段错误。

在这种情况下,您总是希望分配一个新节点。试试这个:

struct NODE *node = malloc(sizeof(struct NODE));

if (node != NULL)
{
strcpy(node->username, cur_username);
strcpy(node->password, cur_pw);
strcpy(node->type, cur_type);

/* push node onto list head */
node->next = head;
head = node;
}

关于c - 段错误 : 11 - C Linked-List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com