What are similarities and differences between Structures and-00998
This subjective question is related to the book/course vu cs605 Software EngineeringII. It can also be found in vu cs605 Mid Term Solved Past Paper No. 2.
In structures, we have different data members and all of these have their own memory space. In union, the memory location is same while the first data member is one name for that memory location. However, the 2nd data member is another name for the same location and so on. Consider the above union (i.e. intOrChar) that contains an integer and a character as data members. What will be the size of this unionThe answer is the very simple. The union will be allocated the memory equal to that of the largest size data member. If the int occupies four bytes on our system and char occupies one byte, the union intOrChar will occupy four bytes
-- OR --Structure
In structures, the data members are public by default. It means that these are visible to all and anyone can change them. Is there any disadvantage of this. Think about the date.
syntaxstruct student {
char name[60];
char address[100];
float GPA;
};
Unions We have another construct named union. The concept of union in C/C++ is: if we have something in the memory, is there only one way to access that memory location or there are other ways to access it. We have been using int and char interchangeably in our programs. We have already developed a program that prints the ACSII codes. In this program, we have stored a char inside an integer. Is it possible to have a memory location and use it as int or char interchangeablyFor such purposes, the construct union is used. The syntax of union is:
union intOrChar {int i;
char c;
};