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.
Array
Structure
Element types
All the same (homogeneous)
May differ (heterogeneous)
Element selected by
Index: a[2]
Member name: s.roll_no
Whole-object assignment with =
Not allowed
Allowed
Typical use
A list of similar values
One 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 2Size 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
Method
Example
Note
Type first, variables later
struct student stud1, stud2;
Most common; the type can be reused anywhere
Type and variables together
struct telephone { ... } tele1, tele2;
Convenient for a small program
No tag
struct { ... } stud1, stud2;
No more variables of this type can be declared later
With typedef
typedef 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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/structures-and-unions/declaring-structures/ to read this page.