Structure And Union - Study Mode
[#111] What will be the output of the following C code? #include <stdio.h>
struct p
{
char *name
struct p *next
}
struct p *ptrary[10]
int main()
{
struct p p, q
p.name = "xyz"
p.next = NULL
ptrary[0] = &p
strcpy(q.name, p.name)
ptrary[1] = &q
printf("%s
", ptrary[1]->name)
return 0
}
Correct Answer
(B) Segmentation fault/code crash
Explanation
Solution: In this code, the variable q is declared but not initialized. When you try to use strcpy function to copy the value of p.name to q.name , you are accessing memory that has not been allocated for q.name , which leads to undefined behavior. This undefined behavior could result in a segmentation fault or a code crash. Therefore, the correct answer is Option B: Segmentation fault/code crash.
[#112] What will be the output of the following C code? #include <stdio.h>
struct student
{
char *c
}
void main()
{
struct student n
struct student *s = &n
(*s).c = "hello"
printf("%p
%p
", s, &n)
}
Correct Answer
(D) Same address
[#113] What is the order for the following C declarations? short a : 17
int long y : 33
Correct Answer
(C) Illegal, illegal
[#114] What will be the output of the following C code? #include <stdio.h>
union u
{
struct
{
unsigned char x : 2
unsigned int y : 2
}p
int x
}
int main()
{
union u u.p = {2}
printf("%d
", u.p.x)
}
Correct Answer
(A) Compile time error
[#115] How do you declare an array of structures in C?
Correct Answer
(B) myStruct array[]