- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Structures
![]() Share with a Friend |
C Programming - C Structures
C Structures
A structure in C is a user-defined data type that allows grouping variables of different types under a single name. It provides a convenient way of handling related data as a single entity and is widely used in programming for defining complex data models.
Defining a Structure
The struct keyword is used to define a structure in C.
Syntax:
C
struct StructureName {
dataType member1;
dataType member2;
// Additional members
};
Example:
C
struct Point {
int x;
int y;
};
Declaring Structure Variables
Method 1: Separate Declaration
C
struct Point p1, p2;
Method 2: Declaration with Definition
C
struct Point {
int x;
int y;
} p1, p2;
Accessing Structure Members
Use the dot (.) operator to access the members of a structure.
Example:
C
struct Point {
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 10;
p1.y = 20;
printf("x = %d, y = %d\n", p1.x, p1.y);
return 0;
}
Nested Structures
Structures can contain other structures as members.
Example:
C
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point bottomRight;
};
int main() {
struct Rectangle rect = {{0, 0}, {10, 10}};
printf("Top Left: (%d, %d)\n", rect.topLeft.x, rect.topLeft.y);
printf("Bottom Right: (%d, %d)\n", rect.bottomRight.x, rect.bottomRight.y);
return 0;
}
Arrays of Structures
You can create an array of structures to store multiple records.
Example:
C
struct Point {
int x;
int y;
};
int main() {
struct Point points[3] = {{0, 0}, {1, 1}, {2, 2}};
for (int i = 0; i < 3; i++) {
printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
}
return 0;
}
Pointer to Structure
A structure pointer is used to access members using the arrow operator (->).
Example:
C
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {10, 20};
struct Point *ptr = &p1;
printf("x = %d, y = %d\n", ptr->x, ptr->y);
return 0;
}
Self-Referential Structures
A structure can have a pointer to another structure of the same type.
Example:
C
struct Node {
int data;
struct Node *next;
};
This is commonly used in data structures like linked lists.
Passing Structures to Functions
Structures can be passed to functions by value or by reference.
By Value:
C
void display(struct Point p) {
printf("x = %d, y = %d\n", p.x, p.y);
}
int main() {
struct Point p1 = {10, 20};
display(p1);
return 0;
}
By Reference:
C
void display(struct Point *p) {
printf("x = %d, y = %d\n", p->x, p->y);
}
int main() {
struct Point p1 = {10, 20};
display(&p1);
return 0;
}
Advantages of Structures
- Allows grouping of related data.
- Supports data abstraction and encapsulation.
- Simplifies program organization.
- Facilitates the implementation of complex data models.
Limitations of Structures
- Cannot have member functions like classes in object-oriented programming.
- No direct support for data hiding.
- Limited to static memory allocation unless combined with pointers.
Conclusion
Structures are a foundational feature of the C language, enabling efficient and organized handling of complex data. They pave the way for implementing advanced data structures like arrays, linked lists, stacks, and queues.
