Q1. What is the output of the following program? Why? #include main() { typedef
ID: 3535452 • Letter: Q
Question
Q1. What is the output of the following program? Why?
#include
main() {
typedef union {
int a;
char b[10];
float c;
}Union;
Union x,y = {100};
x.a = 50;
strcpy(x.b,"hello");
x.c = 21.50;
printf("Union x : %d %s %f n",x.a,x.b,x.c);
printf("Union y : %d %s %f n",y.a,y.b,y.c);
}
Q2. Given the function below. What would happen if the first parameter would be
struct node *list instead of struct node **list? Explain why.
void add_to_list(struct node **list, int n)
{
struct node *new_node;
new_node = malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Error: malloc failed in add_to_list ");
exit(EXIT_FAILURE);
}
new_node->value = n;
new_node->next = *list;
*list = new_node;
}
Explanation / Answer
1)
output:
Union x : 1101791232 21.500000
Union y : 100 d 0.000000
The reason for getting this output is the union members share the common memory location which is allocated to the largest data member.
2)
When **list changed to *list the the lines of code that we have to change is shown below
new_node->value = n;
new_node->next = list;
list = new_node;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.