Management of structures. How to create one inside another

1

I want the user to add the data of the books and then save them in the library. Any advice to work with two structures? I leave what I advanced so far.

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

//Estructura del libro.
typedef struct{
   char titulo[100];
   char genero[100;
   int paginas;
   int anio_de_edicion;
   char numero_isbn[100];
   char editorial[100];
}Libros;

//Estructura de biblioteca.
typedef struct{
   int Libros vec[100];
}Biblioteca[100];

//Funcion para agregar libros.
void alta(Libros * m){
    printf("Titulo: ");
    gets(m->titulo);
    printf("genero: ");
    gets(m->genero);
    printf("paginas: ");
    scanf("%d", m->&paginas);
    printf("año de edicion: ");
    scanf("%d", m->&anio_de_edicion);
    printf("numero isbn: ");
    gets(m->numero_isbn);
    printf("editorial: ");
    gets(m->editorial);
}
    
asked by P. Matias 12.11.2016 в 19:01
source

1 answer

1

There are two important things when you work with structures that you should not confuse: define a structure and declare a structure.

When defining a structure, what you do is create a new data type (equivalent to int, char or any other that the compiler already knows).

Declaring a structure is reserving memory space for one (or more) structures that were previously defined.

It is important that you understand that a structure is 100% equivalent to any other type of standard C data, and therefore you can declare structures individually, array of structures, pointers to structures, or anything else you normally do with the other types of data.

This is your definition of the book structure:

typedef struct{
   char titulo[100];
   char genero[100;
   int paginas;
   int anio_de_edicion;
   char numero_isbn[100];
   char editorial[100];
}Libros;

And it is perfectly done, there is nothing to add (remember that with this you are not declaring any variable, there is no space in memory reserved for any book)

Now, to create the library, you do not have any need to define a new structure, since the library will simply be a set of books and therefore, with which you declare an array of books will be sufficient, that you must do it from the following way:

Books library [100];

Now you will have an array (or vector) called a library where there will be space for 100 Books.

I hope you serve, greetings!

    
answered by 12.11.2016 / 19:43
source