# 16.8 – Quiz on struct

by on September 20, 2018

1. What would be the output of following program if the address of struct variable is 65500

```#include<stdio.h>
#include<string.h>
struct product
{
char pname[20];
float price;
int qty;
};
int main()
{
struct product x;
strcpy(x.pname,"Lux");
x.price=45.50;
x.qty=12;
printf("%u\t%u\t%u",(unsigned int)x.pname,(unsigned int)&x.price,(unsigned int)&x.qty);
return 0;
}
```
Output
65500   65520   65524
for pname takes 20 bytes and for price takes 4 bytes

2. What would be the output of following program?

```#include<stdio.h>
#include<string.h>
struct product
{
char pname[20];
float price;
int qty;
}x={"Lux",45.50,20};
int main()
{
struct product y,z;
y=x;
z=y;
printf("%s\t%f\t%d",z.pname,z.price,z.qty);
return 0;
}
```
Output:
Lux     45.500000       20

As we can directly assign a struct variable to another struct variable x is assigned to y and y is again assigned to z. Now z will have all the members of x.

3. What would be the output of following program?

```#include<stdio.h>
#include<string.h>
struct num
{
int x;
int y;
}a,*p=&a;
int main()
{
p->x=10;
p->y=a.x+30;
printf("%d\t%d",a.x,a.y);
return 0;
}
```
Output:
10                40

4. What would be the output of following program?

```#include<stdio.h>
struct num
{
int x;
float y;
}p;
int main()
{
if(sizeof(p)==sizeof(p.x)+sizeof(p.y))
printf("Hello");
else
printf("World");
return 0;
}
```
Output:
Hello
The size of struct variable is the sum of sizes of individual variables

5. What would be the output of following program?

```#include<stdio.h>
struct num
{
int x;
float y;
};
int main()
{
struct num p[]={{12,34.50},{45,56.78}};
struct num *q=p;
printf("x=%d\ty=%f",p[0].x,p[0].y);
printf("\nx=%d\ty=%f",q[1].x,q[1].y);
return 0;
}
```
Output:
x=12    y=34.500000
x=45    y=56.779999

In case array, the name of array (p) all ways gives the address of first element, which is being assigned to another pointer of struct num type.

Now, struct array elements can be accessed either using “p” or “q”

6. What would be the output of following program?

```#include<stdio.h>
struct num
{
int x;
};
void change(struct num);
int main()
{
struct num p;
p.x=10;
change(p);
printf("x=%d",p.x);
return 0;
}
void change(struct num q)
{
q.x=q.x+40;
}
```
Output
x=10

sending struct variable follows pass by value, change in formal argument would not change the actual argument

7. What would be the output of following program?

```#include<stdio.h>
struct num
{
int x;
};
void change(struct num*);
int main()
{
struct num p;
p.x=10;
change(&p);
printf("x=%d",p.x);
return 0;
}
void change(struct num *q)
{
q->x=q->x+40;
}
```
Output
x=50

Here the reference of struct variable is send as argument, “q” is the pointer to struct variable. ” q->x=q->x+40;” changes the value of “x” in the “p”

8. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char *p;
};
int main()
{
struct hello *x;
x->p="Codingfox.com";
printf("%s",x->p);
return 0;
}
```
Output:
Codingfox.com

“x->p” is a character pointer, which can be assigned with the address of a string

9. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char *p;
};
int main()
{
struct hello x;
struct hello *y;
y=&x;
x.p="Codingfox.com";
printf("%s",y->p);
return 0;
}
```
Output:
Codingfox.com

10. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char p[];
};
int main()
{
printf("%d",sizeof(struct hello));
return 0;
}
```
Output in Turbo C:
//Error : Size of type is unknown or zero

Output with gcc:
//Error : Flexible array member in otherwise empty struct

As the size of array “p” is not specified, the size would be zero

11. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char *p;
};
int main()
{
printf("%d",sizeof(struct hello));
return 0;
}
```
Output in Turbo C:
2

Output with gcc:
4

It is the size of pointer

12. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char *p;
};
int main()
{
struct hello x[]={"coding","fox"};
printf("%c",x[0].p[2]);
return 0;
}
```
Output
d

x[0].p is “coding” and x[0].p[2] is “d”

13. What would be the output of following program?

```#include<stdio.h>
struct hello
{
char *p;
};
int main()
{
struct hello x[]={"coding","fox"};
printf("%s",&x[0].p[2]);
return 0;
}
```