🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next
Structures in C are user-defined data types that allow you to group different types of data under a single name. They are essential for creating complex data structures and organizing related data.
struct structure_name {
data_type member1;
data_type member2;
// ... more members
};struct Person {
char name[50];
int age;
float height;
char city[30];
};struct Person {
char name[50];
int age;
float height;
};
struct Person person1, person2;struct Person {
char name[50];
int age;
float height;
} person1, person2;typedef struct {
char name[50];
int age;
float height;
} Person;
Person person1, person2; // No need for 'struct' keywordstruct Person person1;
person1.age = 25;
strcpy(person1.name, "John");
person1.height = 175.5;struct Person *ptr = &person1;
ptr->age = 25; // Same as (*ptr).age = 25
strcpy(ptr->name, "John");
ptr->height = 175.5;struct Person person1 = {"John", 25, 175.5};struct Person person1 = {
.name = "John",
.age = 25,
.height = 175.5
};struct Person person1 = {0}; // All members set to 0/NULLstruct Address {
char street[100];
char city[50];
char state[30];
int zipcode;
};
struct Person {
char name[50];
int age;
struct Address address;
};
// Accessing nested members
struct Person person1;
strcpy(person1.address.city, "New York");
person1.address.zipcode = 10001;struct Node {
int data;
struct Node *next;
};struct Person people[100];
// Accessing array elements
people[0].age = 25;
strcpy(people[1].name, "Alice");void printPerson(struct Person p) {
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
}
// Function call
printPerson(person1);void updateAge(struct Person *p, int newAge) {
p->age = newAge;
}
// Function call
updateAge(&person1, 26);struct Person createPerson(char name[], int age, float height) {
struct Person p;
strcpy(p.name, name);
p.age = age;
p.height = height;
return p;
}
// Function call
struct Person person2 = createPerson("Alice", 30, 165.0);struct Example1 {
char a; // 1 byte
int b; // 4 bytes
char c; // 1 byte
}; // Total: 12 bytes due to padding
struct Example2 {
char a; // 1 byte
char c; // 1 byte
int b; // 4 bytes
}; // Total: 8 bytes (more efficient)#pragma pack(1)
struct Packed {
char a; // 1 byte
int b; // 4 bytes
char c; // 1 byte
}; // Total: 6 bytes (no padding)
#pragma pack()struct Student {
int roll_no;
char name[100];
float marks[5];
float average;
};
void calculateAverage(struct Student *s) {
float sum = 0;
for(int i = 0; i < 5; i++) {
sum += s->marks[i];
}
s->average = sum / 5.0;
}
void displayStudent(struct Student s) {
printf("Roll No: %d\n", s.roll_no);
printf("Name: %s\n", s.name);
printf("Marks: ");
for(int i = 0; i < 5; i++) {
printf("%.2f ", s.marks[i]);
}
printf("\nAverage: %.2f\n", s.average);
}struct Date {
int day;
int month;
int year;
};
struct Book {
char title[100];
char author[100];
char isbn[20];
float price;
struct Date publish_date;
int pages;
char category[50];
};
void addBook(struct Book *library, int *count, struct Book newBook) {
library[*count] = newBook;
(*count)++;
}
void searchByAuthor(struct Book *library, int count, char author[]) {
for(int i = 0; i < count; i++) {
if(strcmp(library[i].author, author) == 0) {
printf("Title: %s\n", library[i].title);
printf("ISBN: %s\n", library[i].isbn);
printf("Price: $%.2f\n\n", library[i].price);
}
}
}- Use meaningful names for structures and members
- Group related data together in a single structure
- Consider memory layout when designing structures
- Use typedef for cleaner code when appropriate
- Initialize structures properly to avoid undefined behavior
- Pass large structures by reference to avoid copying overhead
- Document structure members with comments for clarity
- Forgetting to include necessary headers (e.g.,
<string.h>forstrcpy) - Accessing uninitialized structure members
- Comparing structures directly (use member-wise comparison)
- Ignoring structure padding in memory-critical applications
- Not checking pointer validity before using arrow operator
Structures are fundamental building blocks in C programming that enable:
- Organization of related data
- Creation of complex data types
- Building sophisticated data structures
- Better code organization and readability
Understanding structures is crucial for advanced C programming, including linked lists, trees, graphs, and other data structures.
🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next