Assume that I want to store some data degrees(int), salary(float). But I know that at any given point of time I will either store degree or salary, but not both. then which of the below data structures I have to use?
we can use any of the option 1 and 2.
we have to use by mixing both option 1 and 2.
when we know that we are going to use only one of the degrees or salary, then why to waste space by allocating memory for both the elements. instead we can allocate one single space that can accommodate either degrees or salary. so preferred to use union in this case.
Back To Top