简体   繁体   中英

How to initialize an array of struct in a struct?

I have a struct with an array of another struct inside it and I am having trouble initialising the struct.

typedef struct stack * Stack;
typedef struct book * Book;

struct book {
    char *title;
    int pages;
};

struct stack {
    int num_books;
    Book array[50]
};

What I am trying to do is to create an empty stack with zero books but I keep getting segmentation faults on everything I tried.

Here is my initialization function:

Stack create_stack(void) {
    Stack s = malloc(sizeof(struct stack) * 50);
    s->num_books = 0;
    // s->array[0]->title = Null;
    // s->array[0]->pages = 0;
    // the above 2 lines give a seg fault: 11
    // I also tried:
    // s->array = s->array = malloc(sizeof(struct book) * 50);
    // Which gives the error that array type 'Book [50]' is not assignable
    return s;
}

How can I create an empty stack with zero books?

You haven't allocated memory for struct book objects. The struct:

struct stack {
    int num_books;
    Book array[50];
};

defines array member as 50 elements array of pointers to book struct (that is, Book is synonym to struct book * ). These are still "wild" pointers, and you need to assign them with allocated struct objects. In other words, by calling:

Stack s = malloc(sizeof(struct stack) * 50);

you have made a room for fifty objects of type struct stack , but inside each of these structs, there is room for struct book pointers, not objects itself.

Like mentioned in comments, typedefing a pointer type is an easy way to obfuscate the code.

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

#define SIZE 2

typedef struct book {
char * title ;
int pages;
} Book; 

typedef struct stack {
int num_book;
Book book_arr[SIZE];
} Stack;

//------------------------------------------------

int main (void ){

Stack s1;
  printf("Enter Number of Books : " );
  scanf("%d",&s1.num_book);
  getchar();

     //BOOK
       for( size_t j = 0 ; j < s1.num_book ; j++ ){
         char temp[100];
         printf("Enter the Book Title for %zd Book : ", (j+1) );
         fgets(temp,100,stdin);
         strtok(temp,"\n");     // for removing new line character
     s1.book_arr[j].title = malloc ( sizeof(temp) +1 );
     strcpy(s1.book_arr[j].title,temp);
                    //  puts(s1.book_arr[j].title );
         printf("Enter Pages for %zd Book : ",(j+1) );
     scanf("%d",&s1.book_arr[j].pages); getchar();
      }
            //PRINT
size_t count = 0 ;
       for( size_t i = 0 ; i < s1.num_book ; i++ ){
     while(count < SIZE ) {
       printf("Book Title : %s\nBook pages : %d\n",s1.book_arr[count].title, s1.book_arr[count].pages );
       free(s1.book_arr[count].title );
       count++;
      } 
}       
return 0;
}

Is that what you were trying to achieve ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM