๐Ÿงฑ C Structures, Unions & Enums
Estimated reading: 3 minutes 361 views

C Structures โ€“ Modeling Real-World Data in C


Introduction โ€“ What Is a Structure in C?

In C programming, a structure (struct) is a user-defined data type that allows grouping variables of different data types under a single name. Structures enable you to create complex data models that represent real-world entities like a student, book, or employee.

In this guide, youโ€™ll learn:

  • How to declare and define structures
  • Access and manipulate structure members
  • Real-world use cases
  • Best practices for memory and function interaction

Core Concept โ€“ Why Use Structures?

Unlike arrays (which store multiple items of the same type), structures let you:

  • Group different types together (e.g., int, float, char[])
  • Model custom data records
  • Build data structures like linked lists, trees, and databases

Code Examples โ€“ Working with Structures

Example 1: Basic Structure Declaration

#include <stdio.h>

struct Person {
    char name[50];
    int age;
};

int main() {
    struct Person p1 = {"Alice", 30};
    printf("Name: %s\n", p1.name);
    printf("Age: %d\n", p1.age);
    return 0;
}

Example 2: Accessing and Modifying Structure Members

struct Product {
    int id;
    float price;
};

int main() {
    struct Product item;
    item.id = 101;
    item.price = 49.99;

    printf("ID: %d\n", item.id);
    printf("Price: $%.2f\n", item.price);
    return 0;
}

Structure Syntax Breakdown

struct StructName {
    dataType member1;
    dataType member2;
    ...
};

Use struct StructName var; to create a variable of that structure.


Structures with Functions

You can pass structures to functions by:

  • Value (copies the entire structure)
  • Pointer (efficient and modifiable)
void display(struct Person p) { ... }
void modify(struct Person *p) { ... }

Use Cases for Structures

ApplicationUse Case
Database SystemsRecords for users, products, orders
Game DevelopmentEntities: players, enemies, scores
NetworkingPackets, headers, protocols
Embedded SystemsSensor data, device registers

Best Practices & Tips

Best Practice:
Use meaningful structure and member names for readability.

Tip:
Use typedef with structures to simplify declarations:

typedef struct {
    char title[50];
    int pages;
} Book;

Pitfall:
Avoid passing large structures by valueโ€”use pointers to reduce memory usage and increase performance.


Real-World Applications

  • File metadata structures
  • Linked lists (struct Node { int data; struct Node *next; };)
  • Symbol tables and parsers
  • Device configuration data in low-level programming

Summary โ€“ Recap & Next Steps

Structures are the foundation of custom data modeling in C. They allow bundling of logically related variables into a single unitโ€”ideal for building efficient, readable, and modular programs.

Key Takeaways:

  • Structures group different data types under one name
  • Access members using dot (.) or arrow (->) for pointers
  • Pass by pointer for performance and modifiability
  • Enable complex system modeling and memory-efficient data structures

Real-World Relevance:

Used extensively in file systems, operating systems, databases, and network protocols.


Frequently Asked Questions (FAQ)

What is a structure in C?

A user-defined type that groups multiple related variables of different data types under one label.


How do I access structure members?

Use the dot operator (.) for normal variables and arrow operator (->) for pointers to structures.


Can structures be passed to functions?

Yes, by value (copy) or by pointer (reference). Use pointers for efficiency.


Can I declare an array of structures?

Yes. For example:

struct Student students[50];

Can a structure contain another structure?

Yes. This is called a nested structure.


Share Now :
Share

๐Ÿงฑ C Structures

Or Copy Link

CONTENTS
Scroll to Top