Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在结构中初始化结构数组?_C_Arrays_Struct - Fatal编程技术网

C 如何在结构中初始化结构数组?

C 如何在结构中初始化结构数组?,c,arrays,struct,C,Arrays,Struct,我有一个结构,其中包含另一个结构的数组,初始化该结构时遇到问题 typedef struct stack * Stack; typedef struct book * Book; struct book { char *title; int pages; }; struct stack { int num_books; Book array[50] }; 我想做的是创建一个没有书的空堆栈,但是我一直在尝试每一个东西,都会出现分段错误 这是我的初始化函数: S

我有一个结构,其中包含另一个结构的数组,初始化该结构时遇到问题

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

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

struct stack {
    int num_books;
    Book array[50]
};
我想做的是创建一个没有书的空堆栈,但是我一直在尝试每一个东西,都会出现分段错误

这是我的初始化函数:

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;
}

如何创建一个没有书籍的空堆栈?

您尚未为
struct book
对象分配内存。结构:

struct stack {
    int num_books;
    Book array[50];
};
array
成员定义为指向
book
struct的指针的50个元素数组(也就是说,
book
structbook*
的同义词)。这些仍然是“通配符”指针,您需要为它们分配分配已分配的结构对象。换句话说,通过调用:

Stack s = malloc(sizeof(struct stack) * 50);
您已经为50个
struct stack
类型的对象留出了空间,但在每个结构中,都有空间放置
struct book
指针,而不是对象本身

如注释中所述,类型定义指针类型是混淆代码的一种简单方法。

\include
#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;
}
#包括 #包括 #定义大小2 typedef结构书{ 字符*标题; 整版; }书; typedef结构堆栈{ int num_书; 书的大小; }堆叠; //------------------------------------------------ 内部主(空){ 堆栈s1; printf(“输入图书数量:”); scanf(“%d”和s1.num_book); getchar(); //书 对于(大小j=0;j

这就是您试图实现的目标吗?

您需要将堆栈malloc为
sizeof(struct Stack)
。由50本书组成的“数组”(由于typedef而成为指针)是
堆栈的一部分。这只会引起混乱。特别是,看起来像50本书的数组实际上只是50个指针的数组。因此,在使用这些指针之前,您需要为它们分配内存。今天您已经问了一个类似的问题!你应该遵循第一个问题的建议,在继续之前先修复缺陷/故障!而且在您的代码中没有“在
struct
中的
struct
数组!”@Olaf typedef的替代方案是什么?例如,如果我不使用类型def,create_stack函数的返回类型是什么?如果您不知道,您应该首先了解
typedef
的作用。不要盲目地遵循你不理解的模式。绝对不需要
typedef
。正如其他人和我已经写的,这是一个严格的禁止!