0

I am supposed to Implement a structure-based Library Information System. I have to create many structures including the following two structures(SBook and ABookList) with the different variables in both the structures. I want to ask that can we have a structure(SBook) as the data type of a variable or array(books) in another structure(ABookList)? see the code below. I have implemented and its almost working in my case but I did it by experiment, I want to know the concept behind it. Thanks.

Code:

  • 5
    You can have any type as a structure member, except itself. The concept behind it is that user-defined types are treated the same as built-in types. This was a novel idea in the late 1950s – early 1960s, but is generally taken for granted these days. – molbdnilo Apr 16 '20 at 13:14
  • 1
    Please post code as text into the question. Don't link to external resources. I recommend [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Thomas Sablik Apr 16 '20 at 13:14
  • 1
    your code already does what you are asking for. Why do you have doubts whether this can be done? – 463035818_is_not_an_ai Apr 16 '20 at 13:16
  • 1
    also your `sbook` does contains `string`s and `std::string` is no different from a type you define yourself – 463035818_is_not_an_ai Apr 16 '20 at 13:17
  • there are several reasons why code should not be posted as images. For one see [Why is this program erroneously rejected by three C++ compilers?](https://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers), more seriously, not everybody can view images – 463035818_is_not_an_ai Apr 16 '20 at 13:23

1 Answers1

0

Maybe you don't need a struct that contains only an array of another struct (it seems an overkill), you can do something simpler like so:

struct book
{
   int id;
   string bookName;
}

and when you need an array of book instanciate an array like so:

book arrayOfBooks[10];
SeventhSon84
  • 364
  • 1
  • 4