gpt4 book ai didi

c - 将数据写入动态数组时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:17 26 4
gpt4 key购买 nike

我的任务是编写一个文件,显示用户输入的未知数量的记录。每条记录都有以下字段:名字、姓氏、地址、城市、州、邮政编码和电话号码。

我假设最好的方法是用上面的字段定义一个结构 Record,然后声明一个 Record 数组,其中包含与用户输入的记录一样多的记录。为了实现这一点,我将使用一个循环来获取每条记录的每个字段的输入,然后如果用户想继续动态地在 Record 数组中分配一个额外的空间并继续直到用户输入 no。我在以下行遇到访问冲突写入位置错误:

scanf("%s", records[i]->fname);

我的代码有什么问题?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Record;

struct Record
{
char fname[51];
char lname[51];
char address[51];
char city[51];
char state[51];
int zipcode;
int phoneNumber;
};

int main()
{
FILE *fileWriter;
const char filename[] = "data.txt";
char answer = 'y';
int size = 1;
int i = 0;
struct Record **records;
records = malloc(sizeof(*records)*(size));

while(answer == 'y' || answer == 'Y')
{
printf("First Name: \n");
scanf("%s", records[i]->fname);

printf("Last Name: \n");
scanf("%s", records[i]->lname);

printf("Address: \n");
scanf("%s", records[i]->address);

printf("City: \n");
scanf("%s", records[i]->city);

printf("State: \n");
scanf("%s", records[i]->state);

printf("Zipcode: \n");
scanf("%d", records[i]->zipcode);

printf("Phone Number: \n");
scanf("%d", records[i]->phoneNumber);
//stores all record info

printf("Are there anymore records? [y/n] ");
answer = getchar();
if(answer == 'y' || answer == 'Y')
{
size++;
records[i++];
printf("\n");
}
records = realloc(records,sizeof(*records)*(size));
}

//open file
fileWriter = fopen(filename,"wb");

if(fileWriter != NULL)
{
if(fwrite(records,sizeof(*records),size,fileWriter) != 1)
{
fprintf(stderr, "Failed to write to %s\n", filename);
exit(1);
}
fclose(fileWriter);
}
else
{
printf("Error opening file.");
}
}

编辑版本

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

struct Record
{
char fname[51];
char lname[51];
char address[51];
char city[51];
char state[51];
int zipcode;
int phoneNumber;
};




int main()
{
FILE *fileWriter;
const char filename[] = "data.txt";
char answer = 'y';
int size = 1;
int i = 0;
struct Record *records = NULL;
struct Record *records_temp;




while(answer == 'y' || answer == 'Y')
{
struct Record *records_temp = realloc(records,(size)*sizeof(*records));

if(records_temp == NULL)
{
free(records);

}
records = records_temp;
printf("First Name: \n");
scanf("%s", records[i].fname);
printf("Last Name: \n");
scanf("%s", records[i].lname);

printf("Address: \n");
scanf(" %[^\n]", records[i].address);

printf("City: \n");
scanf("%s", records[i].city);

printf("State: \n");
scanf("%s", records[i].state);

printf("Zipcode: \n");
scanf("%d", &records[i].zipcode);

printf("Phone Number: \n");
scanf("%d", &records[i].phoneNumber);
//stores all record info

printf("Are there anymore records? [y/n] ");
answer = getchar();
if(answer == 'y' || answer == 'Y')
{
size++;
records[i++];
printf("\n");
}

//open file

fileWriter = fopen(filename,"wb");

if(fileWriter != NULL)
{
if(fwrite(records,sizeof(*records),size,fileWriter) != 1)
{
fprintf(stderr, "Failed to write to %s\n", filename);
exit(1);
}
fclose(fileWriter);
}
else
{
printf("Error opening file.");
}
}
}

最佳答案

嗯,你会遇到段错误,因为你还没有为你的记录中的第一个实体分配内存。

所以要解决这个问题你需要

records[size-1] = malloc(sizeof(Records));

换句话说:您有 records,它是指向 Records 的指针。当你做的时候

records = malloc(sizeof(*records)*(size));

您实际上要求 size 指向 Records 的指针。但这还不够,您需要分配另一 block 内存来存储实际的 Records 因此我们必须

records[size - 1] = malloc(sizeof(Records));

注意:如果 size > 1 那么你应该这样做:

int i = 0;
for(;i < size; i++) {
records[i] = malloc(sizeof(Records));
}

除此之外,您为什么选择 Records **,如 Arjun已经解释过了,你应该使用Records * 并修复realloc 部分-ing 新内存,因为如果realloc 失败,它返回NULL 并且在最坏的情况下您最终会出现内存泄漏或另一个段错误,无论哪种方式 - 这对您的程序都不利。

请参阅Arjun's post

关于c - 将数据写入动态数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448605/

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