3 Structure s Choose the best answer on the following quest
3- ( Structure s) Choose the best answer on the following questions:
(I). Which of the following accesses a variable in structure b?
A. b->var;
B. b.var;
C. *b-var;
D. b>-var;
(II). Which of the following accesses a variable in a pointer to a structure, *b?
A. b->var;
B. b.var;
C. *b-var;
D. *b>var;
(III). Which of the following is a properly defined struct?
A. struct {int a,b;}
B. struct a_struct {int a,b; }
C. struct a_struct int a,b;
D. struct a_struct {int a;};
(IV). Which properly declares a variable of struct foo?
A. struct foo;
B. struct foo var;
C. foo;
D. int foo;
(V) What is the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. struct student
5. {
6. int no;
7. char name[20];
8. };
9. struct student s;
10. no = 8;
11. printf(\"%d\", no);
12. }
a) Nothing
b) Compile time error
c) Junk
d) 8
How do you update the code above to correct the output.
Solution
(3)
(i)
1. Operator (.) is used for accessing any member of a structure.
2. Operator(.) is placed between the structure variable name and member of structure willing to access.
In the given example, b is a structure variable name.
In order to access member var using b make use of the following syntax:
b.var
Hence the correct option is B
_______________________________________________________________________________________________
(ii)
1. Operator (->) is used for accessing any member of a structure (if structure variable is a pointer).
2. Operator(->) is placed between the pointer structure variable name and member of structure willing to access.
In the given example, b is a pointer structure variable name.
In order to access member var using b make use of the following syntax:
b->var
Hence the correct option is A
_______________________________________________________________________________________________
(iii)
Every structure definition follows the following syntax:
struct [Name of Structure]
{
Member definition;
Member definition;
...
Member definition;
} [any number of structure variables separated by comma];
1. struct is a key-word, followed by name of the structure.
2. With in the braces, member variable definitions are placed
3. At the end of structure definition, semicolon is placed.
In the given options, option (D) follows the correct syntax, which creates a structure by name a_struct with an integer member var
Hence the correct option is (D)
_______________________________________________________________________________________________
(iv)
Declaring a variable of struct foo requires the following syntax:
struct structName variableName
In the given options, option (B) has the correct syntax (struct foo var)
Hence the correct option is (B)
_______________________________________________________________________________________________
(v)
Given code generates a compile time error (no: undeclared identifier) .
Variable \"no\" is a member of structure by name student. It should be accessed using dot(.) operator.
Hence the correct option is (b)
_______________________________________________________________________________________________
The code can be made correct by just placing structure name followed by dot operator before member variable \"no\"
Updated code:
#include <stdio.h>
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
//Observe the syntax
s.no = 8;
//Observe the syntax
printf(\"%d\", s.no);
}
Now, this code works correctly and displays value 8 on to screen.


