Mastering C Structs: Definition, Variables, Access & Advanced Examples
Mastering C Structs
Learn how to define, create, and use C structs with clear examples and best practices.
What Is a C Struct?
A struct (short for structure) is a composite data type that groups variables of different types under a single name. It’s the foundation for creating complex data structures in C.
Defining a Structure
Before you can create variables of a struct type, you must define the type. The struct keyword introduces the definition:
struct structureName {
dataType member1;
dataType member2;
…
};
Example:
struct Person {
char name[50];
int citNo;
float salary;
};
This creates a new type called struct Person. You can now declare variables of that type.
Creating Structure Variables
Defining a struct type does not allocate memory. To work with it, declare variables:
struct Person {
// members
};
int main() {
struct Person person1, person2, p[20];
return 0;
}
Alternatively, combine the definition and variable declaration:
struct Person {
// members
} person1, person2, p[20];
person1andperson2are individualstruct Personvariables.pis an array of 20struct Personinstances.
Accessing Struct Members
Two operators retrieve members:
.– Member access operator.->– Pointer-to-struct member operator (covered in a separate tutorial).
To read or modify the salary of person2:
person2.salary
Example 1: Using a Struct in C
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int citNo;
float salary;
} person1;
int main() {
strcpy(person1.name, "George Orwell");
person1.citNo = 1984;
person1.salary = 2500;
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);
return 0;
}
Output
Name: George Orwell Citizenship No.: 1984 Salary: 2500.00
Notice strcpy() copies the string into the fixed-size char array because the assignment operator cannot be used directly with arrays.
Using typedef with Structs
The typedef keyword creates an alias for a type, simplifying declarations:
typedef struct Distance {
int feet;
float inch;
} distances;
int main() {
distances d1, d2;
}
Without typedef, you’d write:
struct Distance d1, d2;
Example 2: Alias in Practice
#include <stdio.h>
#include <string.h>
typedef struct Person {
char name[50];
int citNo;
float salary;
} person;
int main() {
person p1;
strcpy(p1.name, "George Orwell");
p1.citNo = 1984;
p1.salary = 2500;
printf("Name: %s\n", p1.name);
printf("Citizenship No.: %d\n", p1.citNo);
printf("Salary: %.2f", p1.salary);
return 0;
}
Output
Name: George Orwell Citizenship No.: 1984 Salary: 2500.00
Nested Structures
Structs can contain other structs, enabling hierarchical data modeling:
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integers;
} num1, num2;
To set imag of num2 to 11:
num2.comp.imag = 11;
Example 3: Nested Structs in Action
#include <stdio.h>
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {
num1.comp.imag = 11;
num1.comp.real = 5.25;
num1.integer = 6;
printf("Imaginary Part: %d\n", num1.comp.imag);
printf("Real Part: %.2f\n", num1.comp.real);
printf("Integer: %d", num1.integer);
return 0;
}
Output
Imaginary Part: 11 Real Part: 5.25 Integer: 6
Why Use Structs in C?
Suppose you need to store a person’s name, citizenship number, and salary. Without structs, you’d create separate variables for each field for every person, quickly leading to code duplication and confusion. Structs encapsulate related data into a single logical unit, promoting cleaner code and easier maintenance.
What’s Next?
- Structs and pointers
- Passing structs to functions
C Language
- C# Hello World – Building Your First C# Application
- Understanding C# Keywords and Identifiers: Rules, Lists, and Best Practices
- Master C# Variables & Primitive Data Types: A Complete Guide
- C# Operators – Comprehensive Guide to Operators in C#
- Mastering C# Operator Precedence & Associativity: A Practical Guide
- Understanding C# Structs: Definition, Usage, and Key Differences
- Mastering C Structs: Definition, Variables, Access & Advanced Examples
- Mastering C Structs and Pointers: A Practical Guide
- Mastering C Structs: Passing, Returning, and Reference Handling
- C++ Structs Explained with a Practical Example