- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Structures and Functions in C
In C programming, struct is a derived data type. Just as we can pass arguments of primary data types, a variable of struct data type can also be passed to a function. You can also pass structures using call by value and call by reference methods. A function in C may also return a struct data type.
Read this chapter to understand the following concepts −
- How to pass elements of struct type
- How to pass a struct variable
- How to return struct from a function
- How to return a struct pointer
Let's start with the first one and learn how to pass elements of struct type.
How to Pass Struct Elements
A derived type is a combination of one or more elements of any of the primary types as well as another derived type. It is possible to pass elements to a function, either by value or by reference.
Example
In the following example, we have a derived type called "rectangle" with two elements. We have a struct variable "r" with the elements "r.len" and "l.brd" and they are passed to a function. The area() function then computes the area of the rectangle.
#include <stdio.h> struct rectangle{ float len, brd; }; int area(float, float); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(r.len, r.brd); return 0; } int area(float a, float b){ double area = (double)(a*b); printf("Length: %f \nBreadth: %f \nArea: %lf\n", a, b, area); return 0; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Pass a Struct Variable
Let us modify the above example to pass the struct variable itself (instead of its elements) to the area() function. The rectangle struct type also has an additional element called "area".
Example
Inside the function, the elements of the struct variable are accessed though the dot operator (.) and the area is calculated.
#include <stdio.h> struct rectangle{ float len, brd; double area; }; int area(struct rectangle); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(r); return 0; } int area(struct rectangle r){ r.area = (double)(r.len*r.brd); printf("Length: %f \nBreadth: %f \nArea: %lf\n", r.len, r.brd, r.area); return 0; }
Output
Run the code and check its output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Return Struct from a Function
We know that a function in C can return a value of any type. In this example, the area() function is defined to return a struct variable.
Example
Inside the main() fuction, the inputs for the length and the breadth are passed to the area() function. Inside the area() function, the area is computed and a struct variable is populated and returned to the main() function, where its elements are displayed.
#include <stdio.h> struct rectangle { float len, brd; double area; }; struct rectangle area(float x, float y); int main(){ struct rectangle r; float x, y; x = 10.5; y = 20.5; r = area(x, y); printf("Length: %f \n Breadth: %f \n Area: %lf\n", r.len, r.brd, r.area); return 0; } struct rectangle area(float x, float y){ double area = (double)(x*y); struct rectangle r = {x, y, area}; return r; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Pass a Struct by Reference
In C language, a function may be defined to have its arguments passed by value or reference. A reference is the pointer to an existing variable.
Example
In this example, a struct variable of "rectangle" type is declared in main() and its address is passed to a user-defined function called area().
When the area() function is called, it can use the elements of the variable with the indirection operator (→). It computes the result and assigns it to the area element "r → area".
#include <stdio.h> struct rectangle{ float len, brd; double area; }; int area(struct rectangle *); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(&r); return 0; } int area(struct rectangle *r){ r -> area = (double)(r -> len * r -> brd); printf("Length: %f \nBreadth: %f \nArea: %lf\n", r -> len, r -> brd, r -> area); return 0; }
Output
Run the code and check its output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Return a Struct Pointer
Let us rewrite the above code to define the area() function and return a pointer to a struct of rectangle data type.
Example
The area() function has two call-by-value arguments. The main() function reads the length and breadth from the user and passes them to the area() function, which populates a struct variable and passes its reference back to the main() function.
#include <stdio.h> struct rectangle { float len, brd; double area; }; struct rectangle * area(float x, float y); int main (){ struct rectangle *r; float x, y; x = 10.5; y = 20.5; r = area(x, y); printf("Length: %f \n Breadth: %f \n Area: %lf\n", r->len, r->brd, r->area); return 0; } struct rectangle * area(float x, float y){ double area = (double)(x*y); static struct rectangle r; r.len = x; r.brd = y; r.area = area; return &r; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000