In the previous chapter, we learned about structure variables, but in this chapter, we will dig deeper into the same and try to learn how to declare a structure variable, its purpose and how to use it in a C++ program.

So, let us start where we left off, with our example in the previous chapter.

struct Books {

   char  title[50];

   int no_of_pages;

   int no_of_copies;

   char  author[50];

   char  category[100];

   int   book_id;

} book;

book, as we know here is a structure variable. Now, when we declare struct Books, in our C++ program, then we are actually defining a way to store Book type of memory.

That seems pretty complex, but it is actually pretty easy. When you declare an integer type of variable, then you try to reserve a chunk of memory, that will store the integer type of data, and that memory is referred to by the variable name.

int a=10;

Here, a will be referring to a memory location that holds 10, which is an integer value. Integer is a predefined data type in C++.

But, C++ provides you with the freedom to declare your own data types, known as the user-defined data types. These are declared as per the need of the programmer. Now, the structure is a similar user defined data type.

So, when you declare the structure Book, then you define that what form will your data type will take, in our example, it holds, three-character arrays and three integer values.

Now, book the structure variable points to a memory location, that holds the three-character arrays and three integer values, thus making book, a variable of type Book (the structure). The total memory occupied by book, variable will be the sum of the memory occupied by individual data types, in this case it will be 106 bytes.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019