MCS-201 · Unit 6

Structures and Unions

Section 2 of 5

Block 2 · Unit 6Structures and Unions13 min · Section 2 of 5

6.2Accessing and Initialising Structure Members

Covers IGNOU MCS-201 §6.3, 6.4

A structure variable is useful only if you can put values into its members and read them back. This section covers the dot operator, initialisation in the declaration, structures inside structures, and what can and cannot be done with a structure as a whole.

The dot operator

Syntax
structure-variable.member-name

The dot operator (.), also called the member operator, selects one member. The result behaves exactly like an ordinary variable of the member’s type: s1.roll_no is an int, so it can be assigned, used in arithmetic, or read with scanf("%d", &s1.roll_no). A string member such as s1.name is an array, so it is read with %s and no &, and set with strcpy.

Initialising a structure

A structure can be initialised in its declaration with values in braces, listed in the order of the members:

Syntax
struct tag variable = { value1, value2, ... };
  • Members without a value are set to zero.
  • Since C99, members can be named: struct sale s = { .amt = 600.0f, .item = "toolkit" }; (a designated initialiser).

Nested structures

A member of a structure can itself be a structure. This is a nested structure. Inner members are reached with two dots: mysale.buyer.firm.

Assigning and comparing whole structures

OperationAllowed?How
AssignmentYesb = a; copies every member, including arrays inside
Comparison with == or !=NoCompare the members one by one
SizeYessizeof(struct tag) or sizeof variable

Why no ==? A structure may contain padding bytes with unpredictable contents, so a byte-by-byte comparison could report two equal records as different, and C does not generate a member-by-member comparison automatically.

Key takeaways

  • The dot operator selects a member: s1.roll_no.
  • Initialise with { ... } in member order; unlisted members become zero.
  • Nested structures use nested braces and chained dots.
  • = copies a whole structure; == is not allowed; sizeof may include padding.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which operator accesses a member through a structure variable?

Show answer

Answer: C. .

The dot operator is used with a structure variable. The arrow -> is used with a pointer to a structure (Unit 7).

Question 2 · multiple choice

After struct point { int x; int y; } p = { 5 };, what is p.y?

Show answer

Answer: D. 0

Members without an initialiser are set to zero when at least one initialiser is given.

Question 3 · multiple choice

Given struct sale containing struct customer buyer, how is the firm name of variable s accessed?

Show answer

Answer: B. s.buyer.firm

Each level of nesting needs its own dot: first the member buyer, then its member firm.

Question 4 · multiple choice

Which statement is valid for two variables a and b of the same structure type?

Show answer

Answer: A. b = a;

Whole-structure assignment is allowed. Comparison and arithmetic operators are not defined for structures.

Question 5 · multiple choice

What does this program print?

C · copy_struct.c
#include <stdio.h>

struct pt { int x; int y; };

int main(void)
{
    struct pt a = { 3, 4 };
    struct pt b = a;
    b.x = 10;
    printf("%d %d\n", a.x, b.x);
    return 0;
}
Show answer

Answer: C. 3 10

b = a makes an independent copy, so changing b.x does not affect a.x.

Question 6 · short answer

Declare a structure date with integer members day, month and year together with a variable today initialised to 15 August 2026, and write the statement that prints it as 15/8/2026.

Show worked answer

struct date { int day; int month; int year; } today = { 15, 8, 2026 }; declares the type and the variable in one statement, with the values assigned in member order. To print: printf("%d/%d/%d\n", today.day, today.month, today.year);, which outputs 15/8/2026.

↑ ↓ move↵ openTab switch type