C Structures

C Tutorial

1. What is a C Structures?

  • A C Structures is a user-defined data type in C.

  • It allows you to store multiple variables of different data types under a single name.

  • Useful for representing complex objects like a student, employee, or point in 2D space.

Syntax:



2. Example: Defining a Structure


 

Output:

ID: 101
Name: Alice
Marks: 95.50

Use strcpy() from <string.h> to assign string values to char arrays.


3. Declaring Structure Variables


  • You can also declare and define together using typedef (simpler):


 


4. Accessing Structure Members

  • Use the dot . operator for normal variables:


  • For pointers to structures, use arrow -> operator:



5. Example: Structure Pointer


 

Output:

x = 10, y = 20

6. Nested Structures

  • Structures can contain other structures:


 

Output:

Name: Alice
DOB: 15-8-2005

7. Array of Structures


 

Output:

ID: 101, Name: Alice, Marks: 95.50
ID: 102, Name: Bob, Marks: 89.00

8. Key Points About C Structures

  1. Structures group different data types into a single entity.

  2. Access members using dot . (normal) or arrow -> (pointer).

  3. Use typedef to simplify structure names.

  4. Can have nested structures and arrays of structures.

  5. Structures are commonly used in records, objects, and complex data handling.

You may also like...