Pages

Structs vs. Arrays



Arrays in Structs

As we know that a struct in C ++ is data type that can store data members of different types. So, as array is not more than a list of elements of a specific data type, so it can also be placed in struct.

Review the following example where an array elementsList of type int is placed in a struct along with its number of elements.

           const int size = 100;
           struct Arrayint{
                 int elementsList[size];
                 int no_of_elements;
           };


           Array list;


Having looked the above statement, lets quickly go through the following statements:



list.no_of_elements = 0;      //statement1
list.elementsList[0] = 15;    //statement2
list.no_of_elements++;        //statement3
cout<<list.elementsList[0];   //statement4



The statement1 sets the number of elements in our array (data member of struct) to 1. The next statement (statement2) sets the value of the first index (index 0) to 15. Next statement (statement3) increments the size of the array (size has become 2). statement4 outputs the value of the first index of the array (output shown will be15)


Structs in Arrays

Suppose there are 40 students who Learn C Online. You want to store information of all of the students and you have a struct like one that we have declared previously. Now if you start declaring one variable for each student, it will be much annoying and requires a lot of time to declare 40 different variables. So, as we store data of pre-defined data types (i.e. int, char, float, etc.) in the form of an array, similarly we will store the data of 40 students in an array of structs. Consider the following statement:


           Student studentsData[40]; 


This statement declares an array of 40 components each of type Student.


Structs vs. Arrays

Until now, we have come to know that arrays and structs have similarities in certain ways, but they differ as well.
 

 
Aggregate OperationStructsArrays
ArithmeticNoNo
AssignmentYesNo
ComparisonNoNo
Input/OutputNoNo (except strings)
Passed as parameter to a functionBy value or by referenceBy reference only
Returned as a value from functionYesNo




Structs within a Struct in C ++

Until now, we have seen that how different types of data can be grouped together using arrays and structs. We have used arrays in structs and structs in arrays. Now we will see how we can use struct within struct.

Let’s consider the following info of a student:


            struct Student{
                string firstName;
                string middleName;
                string lastName;

                int cellPhone;
                int fax;
                string email;

                string address;
                int zip;
                string city;
                string state;
                string country;

                int admissionDate;
                string admissionMonth;
                int admissionYear;
                int graduationDate;
                string graduationMonth;
                int graduationYear;
           };



As we can see that many members are grouped into a single struct. There are 17 members in the struct from which some are related to one another. So we form sub-blocks of these components so that it becomes more readable and easy to use. For this purpose, we define some extra structs as follows:


          struct Name{
              string firstName;
              string middleName;
              string lastName;
          };


          struct contactInfo{
              int cellPhone;
              int fax;
              string email;
          };


          struct Address{
              string address;
              int zip;
              string city;
              string state;
              string country;
          };


          struct timeSpent{
              int admissionDate;
              string admissionMonth;
              int admissionYear;
              int graduationDate;
              string graduationMonth;
              int graduationYear;
          };


Now we can define our original struct using these structs:



           struct Student{
               Name studentName;
               contactInfo studentContact;
               Address studentAddress;
               timeSpent studentTime;
           };



The information in this struct is more readable and easy to manage.







 

0 comments:

Post a Comment