c - 如何使结构的成员动态化

标签 c

假设我有一个结构,我想让它的成员动态化。我觉得成员应该是这样的

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 .....

最佳答案

你只为 id malloc 内存一次。您需要为每本书执行此操作:

喜欢:

 // 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)

关于c - 如何使结构的成员动态化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43191066/

相关文章:

c - 如何将二进制数(32 位)转换为十进制数?

c - 删除 C 中的最后一个字符

unsigned char 中的 C 变量参数?

c - 使用 sse 内在函数时如何从循环中跳出?

c++ - 收到错误 'setLocked' cannot be used as function,初学者,不确定如何解决此错误

c - C中fgetc/fputc和fread/fwrite的速度比较

c - 如何理解链表结构中的指针 'next'?

c - 为什么c中会出现这个错误?

c - 复杂结构的哈希

c - 循环中的表无法正确显示数据