Topic 3- Structs
Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Structures #◇Data Abstraction #◇Data Types #◇Searching #◇Structured Data #◇Structured Programming #◇Memory Management
◇Overview
A
struct(structure) in C groups related variables of different types under a single name. It is the primary mechanism for creating custom data types.
◇Key Concepts
struct groups heterogeneous data into one unit Members are accessed with . (direct) or -> (pointer) typedef creates an alias to avoid repeating struct keyword Structs can contain arrays, other structs, and pointers
◇Details / Theory
Defining a Struct
struct Student {
char name[50];
int age;
float gpa;
};
// Declaring a variable
struct Student s1;
Using typedef
typedef struct {
char name[50];
int age;
float gpa;
} Student;
Student s1; // No need for 'struct' keyword
Initialization
Student s1 = {"Alice", 20, 3.85};
// Designated initializers (C99)
Student s2 = {.name = "Bob", .gpa = 3.50, .age = 21};
Accessing Members
// Direct access with dot operator
printf("Name: %s\n", s1.name);
printf("GPA: %.2f\n", s1.gpa);
// Pointer access with arrow operator
Student *ptr = &s1;
printf("Age: %d\n", ptr->age); // Equivalent to (*ptr).age
Arrays of Structs
Student class[30];
// Initialize and access
strcpy(class[0].name, "Alice");
class[0].age = 20;
class[0].gpa = 3.85;
// Traverse
for (int i = 0; i < 30; i++) {
printf("%s: %.2f\n", class[i].name, class[i].gpa);
}
Nested Structs
typedef struct {
int day, month, year;
} Date;
typedef struct {
char name[50];
Date birthday;
} Person;
Person p = {"Alice", {15, 3, 2004}};
printf("Birth year: %d\n", p.birthday.year);
Passing Structs to Functions
// By value (copies entire struct)
void printStudent(Student s) {
printf("%s - %.2f\n", s.name, s.gpa);
}
// By pointer (efficient, allows modification)
void updateGPA(Student *s, float newGPA) {
s->gpa = newGPA;
}
Struct Size and Padding
The compiler may add padding bytes for alignment:
struct Example {
char a; // 1 byte + 3 padding
int b; // 4 bytes
char c; // 1 byte + 3 padding
};
// sizeof may be 12, not 6
◇Common Pitfalls
Cannot use == to compare structs — must compare member by member. Cannot directly assign strings to char[] members — use strcpy(). Passing large structs by value is inefficient — use pointers. Forgetting -> when accessing members through a pointer (using . instead).
◇Summary
TL;DR —
structgroups variables of different types into a custom type. Usetypedeffor cleaner syntax. Access members with.(direct) or->(pointer). Pass by pointer for efficiency. Combine with arrays for collections of records.
◇Links
Related: ◇What is an Array — ◇Strings — ◇File Handling — ◇Declaration — ◇Element Type Source / Reference:
◇References & Navigation
- Previous Topic: ◇Strings
- Next Topic: ◇File Handling
- Related Notes: ◇Data Types, ◇Abstract Data Types
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
What is the primary purpose of a struct in C?
Which keyword can be used to create an alias for a struct to avoid repeating the struct keyword?
How do you access a member of a struct using a direct variable?
How do you access a member of a struct when you have a pointer to the struct?
What is the correct syntax for C99 designated initializers for a struct Student?
Why might the sizeof a struct be larger than the sum of the sizes of its members?
Which of the following is true about nested structs?
How should you assign a string to a char[] member inside a struct after initialization?
What is a key advantage of passing a struct to a function by pointer instead of by value?
Which common pitfall should be avoided when comparing two structs?