Concept of Structure in C: Definition, Syntax & Dot Operator | Unit 4 | BKNMU BCA
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
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).
Structures provide real-world entity modeling based on four foundational characteristics:
1. Mixed Data Types
Unlike arrays, structure members can combine int, float, char, and arrays within a single cohesive data blueprint.
2. Custom Type Template
Declaring a structure creates a reusable layout template (tag), which does not consume RAM until actual variables are instantiated.
3. Direct Field Access
Individual members within a structure variable are accessed directly using the dot (member selection) operator: variable.member.
4. Sequential Allocation
Members are allocated sequentially in memory. Total byte size is the sum of member sizes plus optional compiler alignment padding.
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. |
- 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.
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;
}
Template Parsing
The compiler evaluates the struct blueprint, calculating the byte offset required for each internal field member.
Variable Instantiation
When struct Student s1; executes, RAM allocates sequential space: roll_no (4 bytes) followed by name (30 bytes) and percentage (4 bytes).
Direct Field Dereferencing
Using s1.percentage directs the CPU to fetch data starting from Base_Address + member_offset in constant time $O(1)$.
};) at the end of a structure definition. Omitting it is one of the most frequent syntax mistakes in C exam answers.
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.
- Definition: User-defined data type grouping heterogeneous (different) data elements.
- Keyword: Declared using the
structkeyword. - 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 (
};).