Which of the following operators connects a structure variable and its element?
- (a)Dot operator
- (b)Plus operator
- (c)Minus operator
- (d)Multiply operator
Correct — A, (a) Dot operator. A structure groups several variables, possibly of different types, under one name, and the operator that connects a structure variable to one of its elements is the dot — written between the variable and the member, as in employee.salary or point.x. Declare struct Employee { int id; float salary; }; and then a variable struct Employee e; and the member is reached as e.id or e.salary. The dot is called the member access operator, or the direct member selection operator, and it takes a structure or union variable on its left and the name of a member — not a value, and not an expression — on its right. The same notation reaches the members of a union, and in the object-oriented languages descended from C it reaches the fields and methods of an object. The operator that goes with it is the arrow, written as a hyphen followed by a greater-than sign, which is used when what you hold is a pointer to a structure rather than the structure itself: ptr->salary. The two are exactly related. Since ptr holds an address, the structure it points to is *ptr, so the member could also be written (*ptr).salary — and the parentheses are not decoration. The dot binds more tightly than the dereference, so *ptr.salary would be read as *(ptr.salary), which is a different thing altogether and will not compile against a pointer. The arrow exists to save writing those parentheses, and knowing that it is only shorthand is the point of the pair. In the precedence table of C the dot and the arrow sit in the highest group, with the postfix operators, and they associate left to right — which is what allows a member of a member to be written company.head.salary and read from the left.
- (b)Plus operator — The plus sign is an arithmetic operator. In its binary form it adds two operands, and in its unary form it asserts the sign of one; neither has anything to do with selecting a named field out of a composite variable. A structure member is identified by a name, not computed by an arithmetic operation, so no arithmetic operator can perform the connection. It is worth remembering that in C the arithmetic operators do not work on a whole structure either: two structure variables of the same type may be assigned one to the other, but they cannot be added or compared with the arithmetic and relational operators, and any operation on the contents has to be done member by member through the dot.
- (c)Minus operator — The minus sign is the subtraction operator in its binary form and the negation operator in its unary form. Its only connection with structures is typographical: the arrow operator that reaches a member through a pointer is written as a hyphen immediately followed by a greater-than sign, so a candidate skimming the syntax may half-remember a minus sign in the neighbourhood of a structure member. That is a two-character token in its own right and not the subtraction operator, and in any case the question describes a structure variable, not a pointer to one, so the dot is what applies.
- (d)Multiply operator — The asterisk in C wears two hats, and neither is member selection. As a binary operator it multiplies; as a unary prefix operator it dereferences a pointer, yielding the object the pointer points at. The second of those does appear in the neighbourhood of structures, because the member of a structure held by a pointer can be written as (*ptr).member, but the asterisk there only turns the pointer into the structure — the connection between the structure and its element is still made by the dot that follows. Drop the parentheses and the expression changes meaning entirely, because the dot has higher precedence than the dereference.
A structure is C's mechanism for building a composite type: several variables, which may be of different types, gathered under a single name so that they can be declared, passed and assigned together. It is the ancestor of the record in other languages and of the class in object-oriented ones, and it is how a program models a real entity such as an employee, a date or a point. Two things must be distinguished at once — the structure template, introduced by the struct keyword and giving the type its shape, and the structure variable, which is an actual object of that type occupying memory. Members are reached through two operators. The dot connects a structure variable to a member; the arrow connects a pointer to a structure to a member, and is defined as shorthand for dereferencing the pointer and then applying the dot. Both sit at the top of C's precedence table with the postfix operators and associate from left to right, which is why nested members are written and read left to right. Structures are copied by assignment as a whole, but they cannot be added or compared with arithmetic and relational operators; anything done to their contents is done member by member. Related storage classes complete the picture: a union looks like a structure and uses the same dot notation, but all its members share the same memory, so only one member holds a valid value at a time; and an array of structures combines subscripting with member selection, as in staff[3].salary.
The computer block of this paper mixes questions from four areas — programming constructs, networking, number systems and hardware — and treats each at the level of a first course rather than of a working programmer. That shapes what is worth revising. The Commission asks which operator does a named job, which command performs a named task, which register holds a named thing, and what a number in one base is in another. None of it requires writing code, but all of it requires the precise vocabulary: member access operator, dereference, precedence, register, topology. This item is the easiest kind to secure, because the answer is a single symbol with a single job and the three distractors are arithmetic operators that have no role in selecting a named field at all. Note also that an APFC candidate is being recruited to an organisation that runs one of the largest transactional databases in the country, so a working familiarity with how data is structured and addressed is on the job description as well as on the syllabus.
- The dot is the member access operator: it connects a structure or union variable on its left to the name of a member on its right, as in employee.salary.
- The arrow operator, a hyphen followed by a greater-than sign, reaches a member through a pointer to a structure, and ptr->member is exactly equivalent to (*ptr).member.
- The parentheses in (*ptr).member are required, because the dot has higher precedence than the dereference operator, so *ptr.member would be parsed as *(ptr.member).
- The dot and the arrow sit in the highest precedence group in C, with the postfix operators, and associate from left to right, which is why nested selections such as company.head.salary read from the left.
- Structure variables of the same type may be assigned as a whole, but they cannot be added or compared with the arithmetic and relational operators; their contents are handled member by member.
- A union uses the same dot notation as a structure, but its members share one storage area, so only one of them holds a valid value at any moment.
- Using the dot on a pointer to a structure; a pointer must either be dereferenced first, with parentheses, or the arrow operator must be used
- Writing *ptr.member without parentheses and expecting it to mean the member of the pointed-to structure
- Confusing the structure template with a structure variable — the template reserves no memory, and only a declared variable can appear to the left of a dot
- Assuming two structures can be compared with the equality operator; only member-by-member comparison works
- Treating a union as a structure with the same behaviour, when assigning to one member of a union destroys the value in the others
Programming questions in this paper are single-fact recall items about a named construct — which operator, which keyword, which command — with distractors drawn from the same symbol set. The efficient preparation is a short glossary of operators and their jobs, learnt with the one confusable neighbour attached to each: dot with arrow, asterisk as multiplication with asterisk as dereference, ampersand as bitwise and with ampersand as address-of.
No directly related past PYQ was found.
- practice — not a real PYQ
If ptr is a pointer to a structure variable, which one of the following correctly accesses the member salary?
- (a)ptr.salary
- (b)*ptr.salary
- (c)ptr->salary
- (d)&ptr.salary
Answer(c) ptr->salary — the arrow operator reaches a member through a pointer to a structure and is exactly equivalent to (*ptr).salary. The dot cannot be applied to a pointer directly, and *ptr.salary is parsed as *(ptr.salary) because the dot binds more tightly than the dereference operator.
- practice — not a real PYQ
Which one of the following statements about a union in C is correct?
- (a)Its members are accessed with the arrow operator only
- (b)All of its members share the same memory location, so only one holds a valid value at a time
- (c)It occupies memory equal to the sum of the sizes of its members
- (d)Its members must all be of the same data type
Answer(b) All of its members share the same memory location, so only one holds a valid value at a time — that shared storage is the whole difference between a union and a structure. A union variable is accessed with the same dot operator as a structure, its size is that of its largest member rather than the sum of them, and its members may be of different types.