- 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 and Functions
![]() Share with a Friend |
C Programming - C Structures and Functions
C Structures and Functions
C allows structures to be used in functions, enabling the manipulation and processing of structured data. Structures can be passed to functions by value or by reference (using pointers), depending on the requirements.
Passing Structures to Functions
- By Value
When a structure is passed by value, a copy of the structure is created in the function's memory. Any changes made to the structure inside the function do not affect the original structure.
Example:
C
#include <stdio.h>
struct Point {
int x;
int y;
};
void display(struct Point p) {
printf("Point: (%d, %d)\n", p.x, p.y);
}
int main() {
struct Point p1 = {10, 20};
display(p1); // Passing by value
return 0;
}
Output:
p>Point: (10, 20)
- By Reference
When a structure is passed by reference, a pointer to the structure is passed to the function. Any changes made to the structure inside the function directly affect the original structure.
Example:
C
#include <stdio.h>
struct Point {
int x;
int y;
};
void update(struct Point *p) {
p->x += 10;
p->y += 10;
}
int main() {
struct Point p1 = {10, 20};
printf("Before update: (%d, %d)\n", p1.x, p1.y);
update(&p1); // Passing by reference
printf("After update: (%d, %d)\n", p1.x, p1.y);
return 0;
}
Output:
Before update: (10, 20)
After update: (20, 30)
Returning Structures from Functions
Functions in C can also return structures. This is useful when you want a function to create and initialize a structure.
Example:
C
#include <stdio.h>
struct Point {
int x;
int y;
};
struct Point createPoint(int x, int y) {
struct Point p;
p.x = x;
p.y = y;
return p;
}
int main() {
struct Point p1 = createPoint(10, 20);
printf("Point: (%d, %d)\n", p1.x, p1.y);
return 0;
}
Output:
Point: (10, 20)
Passing Arrays of Structures to Functions
You can also pass arrays of structures to functions for processing multiple records.
Example:
C
#include <stdio.h>
struct Point {
int x;
int y;
};
void displayPoints(struct Point points[], int size) {
for (int i = 0; i < size; i++) {
printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
}
}
int main() {
struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}};
displayPoints(points, 3);
return 0;
}
Output:
Point 1: (1, 2)
Point 2: (3, 4)
Point 3: (5, 6)
Use Case Example: Managing Student Records
Code:
C
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float marks;
};
void displayStudent(struct Student s) {
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
}
int main() {
struct Student s1;
s1.id = 101;
strcpy(s1.name, "Alice");
s1.marks = 85.5;
displayStudent(s1);
return 0;
}
Output:
ID: 101
Name: Alice
Marks: 85.50
Advantages of Using Structures with Functions
- Modularity: Functions encapsulate the logic for processing structures.
- Reusability: Functions can be reused with different structures.
- Readability: Clear separation of logic and data enhances readability.
- Efficiency: Passing by reference avoids copying large structures.
Conclusion
Using structures with functions in C provides a robust way to manage and manipulate complex data. By leveraging functions to operate on structured data, programmers can write cleaner, modular, and reusable code.
