Covers IGNOU MCS-208 §Programme guide topic "Structures" (not in the Unit 2 booklet)
An array holds many values of one type. Real records mix types: a student has a name (text), a roll number (integer) and marks (real number). A structure groups such values under one name. Structures also give a cleaner way to store the triples of a sparse matrix and the terms of a polynomial.
Declaring a structure
Declaring a structure type and variables
struct student { /* the structure type ("tag") is struct student */ char name[20]; /* members */ int roll_no; float marks;};struct student s1 = {"Asha", 101, 78.5f}; /* a variable, initialised */
The declaration of struct student only describes the layout; memory is reserved when a variable such as s1 is declared.
Accessing members
Operator
Used with
Example
. (dot)
a structure variable
s1.roll_no = 102;
-> (arrow)
a pointer to a structure
p->roll_no = 102; means (*p).roll_no = 102;
Structures of the same type can be assigned as a whole (s2 = s1; copies every member), passed to functions and returned from them. For large structures, pass a pointer (often const) to avoid copying.
typedef
typedef gives a type a new name, so you do not have to write struct every time:
typedef for a sparse-matrix triple
typedef struct { int row, col, value;} Triple;Triple t[50]; /* an array of 50 triples */
Size of a structure and padding
The size of a structure is at least the sum of its members’ sizes, and often more. Many processors read an int fastest when its address is a multiple of 4, so the compiler may insert unused padding bytes between members or at the end. Always use sizeof instead of adding member sizes by hand.
Arrays versus structures
Array
Structure
Element types
All the same
May differ
Access
By index: a[i]
By member name: s.name
Index known at run time?
Yes, i can be a variable
No, the member name is fixed in the code
Typical use
Many values of one kind
One record with several fields
Combined
Array of structures: a table of records
Structure containing arrays: char name[20]
Key takeaways
A structure groups named members of possibly different types into one object.
Access members with . on a variable and -> through a pointer (p->m is (*p).m).
Structures can be assigned, passed and returned as a whole; pass large ones by pointer.
typedef creates a short type name; sizeof of a structure may include padding.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Define a structure for a book (title, author, price, year), declare an array of 3 books and write a loop that prints the title of every book costing more than ₹500.
Show worked answer
struct book { char title[50]; char author[30]; float price; int year; };struct book shelf[3]; (filled elsewhere)
for (int i = 0; i < 3; i++) if (shelf[i].price > 500) printf("%s\n", shelf[i].title);
Each element shelf[i] is a structure, and its members are reached with the dot operator.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/arrays-pointers-and-structures/structures/ to read this page.