MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 5 of 8

Block 1 · Unit 2Arrays, Pointers and Structures10 min · Section 5 of 8

2.5Structures

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

OperatorUsed withExample
. (dot)a structure variables1.roll_no = 102;
-> (arrow)a pointer to a structurep->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

ArrayStructure
Element typesAll the sameMay differ
AccessBy index: a[i]By member name: s.name
Index known at run time?Yes, i can be a variableNo, the member name is fixed in the code
Typical useMany values of one kindOne record with several fields
CombinedArray of structures: a table of recordsStructure 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 1 · multiple choice

Given struct student s, *p = &s;, which expression sets the roll number?

Show answer

Answer: B. p->roll_no = 5;

p is a pointer, so use ->. *p.roll_no is parsed as *(p.roll_no) because . binds more tightly than *; the correct long form is (*p).roll_no.

Question 2 · multiple choice

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

Show answer

Answer: C. Array elements all have the same type; structure members may have different types.

Homogeneous vs heterogeneous is the key difference. Members of a structure are also stored in one block of memory (in declaration order, possibly with padding).

Question 3 · multiple choice

On a system where char is 1 byte and int is 4 bytes with 4-byte alignment, what is sizeof of struct { char c; int i; }?

Show answer

Answer: C. 8

3 padding bytes are placed after c so that i starts at offset 4; total 8 (as the program output shows). 5 is the sum of the members, which ignores padding.

Question 4 · multiple choice

What does this program print?

C · q2-struct.c
#include <stdio.h>

typedef struct {
    int coef, exp;
} Term;

int main(void)
{
    Term t[3] = {{4, 3}, {-2, 1}, {7, 0}};
    Term *p = &t[1];
    p->coef = p->coef * 5;
    printf("%d %d %d\n", t[1].coef, p->exp, (p + 1)->coef);
    return 0;
}
Show answer

Answer: A. -10 1 7

p points to t[1], so the change goes into the array: t[1].coef becomes −10. p->exp is 1 and (p + 1) points to t[2], whose coef is 7. “-2 1 7” wrongly assumes p works on a copy.

Question 5 · multiple choice

What does typedef do?

Show answer

Answer: D. It gives an existing type a new name.

typedef struct {...} Triple; lets you write Triple t; instead of repeating the structure definition. No memory is reserved until a variable is declared.

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.

↑ ↓ move↵ openTab switch type