When should you use a union type component in a structured variable?
2A) Write code to accomplish each of the following:
Define a structure called part containing int variable partNumber and char array partName with values that may be as long as 25 characters (including the terminating null character).
Define Part to be a synonym for the type struct part.
2B) Next 3 questions refer to the following type student_t and to variables stu1and stu2.
typedef struct {
char fst_name[20], last_name[20];
int score;
char grade;
} student_t;
...
student_t stu1, stu2;

Respuesta :

Answer:

The answers to 1st question and part 2A is given. However, the 2B is not given complete and thus cannot be answered.

Explanation:

UNION is a keyword used in C Language to have a commonly shared memory that can be used by multiple elements. It is a data-type that allows different data types to use the same shared memory location.

Importance: If you want to minimize the use of memory by sharing it between different datatype variables, then use UNION. For Ex. Let's suppose I want to use 2 variables a and b of type char and int respectively. Now, suppose int takes memory space of 2 bytes and char takes 1 byte then the total amount of memory needed = 2 + 1 = 3 Bytes. But, we want to first make use of a, and once done, we want to declare variable b. We can limit the memory use by declaring a memory space of 2 bytes (max of a and b). First, we can use this space to store the int variable and when done with it, we can use it to store the char variable.

PART 2A

typedef struct{

  int partNum;

  char partName[25];

} partin;

#define Partin partin

ACCESS MORE