MCS-201 · Unit 6

Structures and Unions

Section 1 of 5

Block 2 · Unit 6Structures and Unions9 min · Section 1 of 5

6.1Declaring Structures

Covers IGNOU MCS-201 §6.2

Keeping a student’s roll number, name, course and marks in four unrelated variables works for one student but falls apart for a whole class. A structure ties these pieces together as one record. This section shows how to describe such a record and create variables of that kind.

What a structure is

A structure differs from an array in two ways: its members may have different types, and each member is selected by name rather than by index.

ArrayStructure
Element typesAll the same (homogeneous)May differ (heterogeneous)
Element selected byIndex: a[2]Member name: s.roll_no
Whole-object assignment with =Not allowedAllowed
Typical useA list of similar valuesOne record with several fields

Declaring a structure type

Syntax
struct tag {
    data-type member1;
    data-type member2;
    ...
};
C · student_type.c
#include <stdio.h>

struct student {
    int roll_no;
    char name[20];
    char course[20];
    int marks_obtained;
};

int main(void)
{
    struct student stud1, stud2;      /* two variables of the type */

    stud1.roll_no = 1;
    stud2.roll_no = 2;
    printf("%d %d\n", stud1.roll_no, stud2.roll_no);
    printf("Size of one student: %zu bytes\n", sizeof(struct student));
    return 0;
}
Output
1 2
Size of one student: 48 bytes

The name after struct is the structure tag. The declaration of the type allocates no memory; it is only a template. Memory is allocated when variables such as stud1 are created. Note the semicolon after the closing brace, and that in C the keyword struct must be repeated when declaring variables: struct student stud1;.

Ways to create structure variables

MethodExampleNote
Type first, variables laterstruct student stud1, stud2;Most common; the type can be reused anywhere
Type and variables togetherstruct telephone { ... } tele1, tele2;Convenient for a small program
No tagstruct { ... } stud1, stud2;No more variables of this type can be declared later
With typedeftypedef struct country { ... } Country; then Country india;Country is a one-word name for struct country

Key takeaways

  • A structure groups members of possibly different types under one name.
  • struct tag { ... }; declares a type; struct tag var; creates a variable.
  • typedef gives the structure type a one-word name.
  • The size of a structure is at least the sum of its members; padding may be added.

Practice questions

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

Question 1 · multiple choice

Which statement about a structure type declaration such as struct book { ... }; is true?

Show answer

Answer: B. It defines a template and allocates no memory

The declaration describes the type only. Memory is allocated when a variable of the type is declared.

Question 2 · multiple choice

How is a variable of struct student declared in C (no typedef)?

Show answer

Answer: C. struct student s;

C requires the keyword struct followed by the tag, then the variable name. Leaving out struct is accepted only in C++.

Question 3 · multiple choice

What is the main difference between an array and a structure?

Show answer

Answer: D. Array elements share one type, while structure members may have different types

Arrays are homogeneous and indexed; structures are heterogeneous and accessed by member name.

Question 4 · multiple choice

After typedef struct point { int x; int y; } Point;, which declaration is valid?

Show answer

Answer: A. Point p;

Point is the typedef name, used alone. The tag is point, which needs struct point p;.

Question 5 · multiple choice

On a typical 64-bit system, what does sizeof report for struct { char a; int b; char c; }?

Show answer

Answer: C. 12

The members total 6 bytes, but padding is added after each char so the int is aligned and the total is a multiple of 4.

Question 6 · short answer

Declare a structure type book with a title (up to 49 characters), an author (up to 29 characters), a price and a number of pages, and declare two variables of it.

Show worked answer

struct book { char title[50]; char author[30]; float price; int pages; }; followed by struct book b1, b2;. The arrays are one larger than the longest text to leave room for the null character. The type declaration ends with a semicolon and allocates no memory; the second statement creates two variables, each with its own four members.

↑ ↓ move↵ openTab switch type