说我有一个结构的东西,我想让它的成员动态。我认为成员应该是这样的
char* id;
.....
跟随每个成员的malloc跟着
books->id=(char*) malloc(size);
当我尝试它时,我的程序崩溃了所以我试图理解我做错了什么
这是我认为与我的代码相关的部分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 512
typedef struct Books {
char *id;
char title[maxsize];
char author[maxsize];
char pages[maxsize];
char year[maxsize];
char subject[maxsize];
} book;
char* filename;
int libsize=4;
int bookcount=1;
...
int main(int argc, char* argv[]){
if (argc < 1)
return -1;
filename=argv[1];
FILE* fptr;
char tempstring[maxsize],* token;
int i=0,ch;
book *books;
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this count how many books are in the file
while(ch!= EOF){
ch=fgetc(fptr);
if(ch == '\n')
++bookcount;
}
fclose(fptr);
while(libsize<bookcount){
libsize *= 1.5;
}
books = (book*) malloc(libsize*sizeof(book));
if(books==NULL)
exit(-1);
books->id=(char*)malloc(100);
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this gets all the books into the book array
for(i=0;i<bookcount;i++){
fgets(tempstring,maxsize,fptr);
token=strtok(tempstring,",");
strcpy(books[i].id,token);
token=strtok(NULL,",");
strcpy(books[i].title,token);
token=strtok(NULL,",");
strcpy(books[i].author,token);
token=strtok(NULL,",");
strcpy(books[i].pages,token);
token=strtok(NULL,",");
strcpy(books[i].year,token);
token=strtok(NULL,",");
strcpy(books[i].subject,token);
}
fclose(fptr);
printf("to add a book press 1\n");
printf("to delete a book press 2\n");
printf("to find a book press 3\n");
printf("to print all books press 4\n");
printf("to save library in a file press 5\n");
printf("to add books from a file press 6\n");
printf("to exit press 0\n");
pick(books);
return 1;
}
void pick(book books[]){
char input;
scanf("%c",&input);
switch (input){
case '1':
addbook(books);
break;
case '2':
delbook(books);
break; //pretty sure break isnt needed but it works so...eh
case '3':
srchbook(books);
break;
case '4':
printbooks(books);
break;
case '5':
printbooksf(books);
break;
case '6':
addbookf(books);
break;
case '0':
free(books);
exit (1);
case '\n':
pick(books);
default:
printf("please enter a valid command\n");
pick(books);
break;
}
}
edit2:添加了更多代码
edit3:发现问题是我做的是试图找到指针本身(我认为)所以当我尝试这样做时系统会翻转。 我应该做的是获取结构的每个成员数组
for(i=0;i<bookcount;i++){
books[i].id=(char*)malloc(charcount);
books[i].title=(char*)malloc(charcount);
books[i].author=(char*)malloc(charcount);
books[i].pages=(char*)malloc(charcount);
books[i].year=(char*)malloc(charcount);
books[i].subject=(char*)malloc(charcount);
}
而不是
books->id .....
答案 0 :(得分:0)
malloc
只有id
个内存一次。你需要为每本书做到这一点:
像:
// books->id=(char*)malloc(100); Remove this line
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this gets all the books into the book array
for(i=0;i<bookcount;i++){
fgets(tempstring,maxsize,fptr);
token=strtok(tempstring,",");
books[i].id=(char*)malloc(100); // Add this line
strcpy(books[i].id,token);
<强>顺便说一句强>
这段代码看起来很奇怪:
while(libsize<bookcount){
libsize *= 1.5;
}
books = (book*) malloc(libsize*sizeof(book));
为什么不简单地做:
books = (book*) malloc(bookcount*sizeof(book));
(注意:您不需要投射malloc
)