BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Concept of Structure in C: Definition, Syntax & Dot Operator | Unit 4 | BKNMU BCA

by BCA School •

BKNMU Junagadh  |  BCA Sem 1  |  Unit 4



Concept of Structure in C: Definition, Declaration & Member Access

Complete Guide to User-Defined Heterogeneous Data Types, Dot (.) Operator, Memory Layout, and Structure Variables

BKNMU Junagadh BCA Sem 1 Unit 4 struct Heterogeneous Data Dot Operator
In simple words: An array allows us to group multiple items together, but they must all share the same data type (homogeneous). In real-world computing, entities have mixed attributes—for instance, a student has a roll number (int), name (char[]), and percentage (float). A Structure (defined via the struct keyword) is a user-defined data type in C that binds related variables of different data types together under a single name. This fundamental lesson covers complete concepts for BKNMU BCA Semester 1 (Unit 4).
📖 The 4 Core Principles of C Structures

Structures provide real-world entity modeling based on four foundational characteristics:

Heterogeneous Data

1. Mixed Data Types

Unlike arrays, structure members can combine int, float, char, and arrays within a single cohesive data blueprint.

User-Defined Type

2. Custom Type Template

Declaring a structure creates a reusable layout template (tag), which does not consume RAM until actual variables are instantiated.

Member Operator (.)

3. Direct Field Access

Individual members within a structure variable are accessed directly using the dot (member selection) operator: variable.member.

Aggregate Memory

4. Sequential Allocation

Members are allocated sequentially in memory. Total byte size is the sum of member sizes plus optional compiler alignment padding.

1️⃣ Master Table: Array vs. Structure Key Differences

A critical comparison frequently asked in BKNMU university examinations:

Comparison Parameter Array Data Structure Structure (struct)
Element Nature Strictly homogeneous (all elements must have identical data types). Heterogeneous (members can hold completely different data types).
Access Mechanism Indexed via square brackets and numerical subscripts: arr[i]. Accessed by member identifier using the dot operator: s1.roll_no.
Keyword Required No dedicated keyword used for declaration. Requires the struct keyword for definition.
Memory Allocation Continuous contiguous memory blocks of equal element lengths. Sequential contiguous memory blocks sized per member (+ alignment padding).
Type Classification Derived data type. User-defined derived data type.
ℹ️ Structure Template vs. Variable Instantiation:
  • Definition/Template: struct Student { int roll; char name[20]; float marks; }; — This only defines the blueprint. No memory is allocated in RAM at this stage.
  • Variable Declaration: struct Student s1; — This allocates actual physical memory in RAM based on the cumulative sizes of all declared members.
💻 Practical Code Example: Declaration, Input & Output of a Structure

The following program demonstrates defining a structure for a student record, taking input from the keyboard, and displaying the details:

#include <stdio.h>

// 1. Structure Template Definition (Global Scope)
struct Student {
    int roll_no;
    char name[30];
    float percentage;
};

int main() {
    // 2. Instantiating a structure variable
    struct Student s1;

    // 3. Reading member data using Dot (.) operator
    printf("Enter Student Roll Number: ");
    scanf("%d", &s1.roll_no);

    printf("Enter Student Name: ");
    scanf("%s", s1.name); // Array name itself acts as address

    printf("Enter Student Percentage: ");
    scanf("%f", &s1.percentage);

    // 4. Displaying structure member values
    printf("\n--- Student Record Output ---\n");
    printf("Roll No    : %d\n", s1.roll_no);
    printf("Name       : %s\n", s1.name);
    printf("Percentage : %.2f%%\n", s1.percentage);
    printf("Memory Size: %lu Bytes\n", sizeof(s1));

    return 0;
}
🔄 Step-by-Step Flow: Structure Memory Allocation
1

Template Parsing

The compiler evaluates the struct blueprint, calculating the byte offset required for each internal field member.

2

Variable Instantiation

When struct Student s1; executes, RAM allocates sequential space: roll_no (4 bytes) followed by name (30 bytes) and percentage (4 bytes).

3

Direct Field Dereferencing

Using s1.percentage directs the CPU to fetch data starting from Base_Address + member_offset in constant time $O(1)$.

💡 Semicolon Rule: Never forget the closing semicolon (};) at the end of a structure definition. Omitting it is one of the most frequent syntax mistakes in C exam answers.
⚠️ Structure Padding Note: The total size returned by sizeof(struct_var) may sometimes be slightly larger than the mathematical sum of its individual members. This occurs because the CPU aligns data to word boundaries (e.g. 4-byte or 8-byte boundaries) for faster access speeds.
📝 Quick Revision — Key Exam Points
  • Definition: User-defined data type grouping heterogeneous (different) data elements.
  • Keyword: Declared using the struct keyword.
  • Access Operator: Individual members are accessed using the dot (.) member operator.
  • Memory Allocation: Blueprint consumes 0 bytes; memory is allocated only when variables are declared.
  • Semicolon: The structure definition must always terminate with a semicolon (};).