Structures in C

📘 C 👁 29 views 📅 Dec 22, 2025
⏱ Estimated reading time: 1 min

A structure is a user-defined data type that allows grouping of different data types under a single name.


Need for Structures

  • Store related data together

  • Represent real-world entities

  • Overcome limitations of arrays


Declaration of Structure

struct structure_name { data_type member1; data_type member2; };

Example Declaration

struct student { int roll; char name[20]; float marks; };

Declaring Structure Variables

struct student s1, s2;

Accessing Structure Members

  • Use dot (.) operator

s1.roll = 1;

Example Program

#include struct student { int roll; char name[20]; float marks; }; int main() { struct student s1; s1.roll = 101; printf("Enter name: "); scanf("%s", s1.name); s1.marks = 85.5; printf("Roll: %d\n", s1.roll); printf("Name: %s\n", s1.name); printf("Marks: %.2f", s1.marks); return 0; }

Array of Structures

struct student s[50];

Passing Structure to Function

  • Passed by value or reference

void display(struct student s);

Structure Pointer

struct student *p; p = &s1;

Access members using -> operator:

p->roll;

Nested Structures

Structure inside another structure.


Advantages of Structures

  • Organizes complex data

  • Improves readability

  • Supports data abstraction


Summary

  • Structures store heterogeneous data

  • Members accessed using dot or arrow operator

  • Can be used with arrays, pointers, and functions


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes