Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Organize complex data with C structures — learn how to define, initialize, and use structs, nested types, pointers, and optimize memory layout clearly.
In C, a structure is a composite data type that allows you to group variables of different data types under a single name. Each variable within a structure is referred to as a member or field. Structures are used to represent complex data structures or real-world entities more efficiently.
Table of contents [Show]
To declare a structure, you use the struct keyword followed by the structure's name. Here's the basic syntax:
struct structure_name {
data_type member1;
data_type member2;
// ...
data_type memberN;
};
Example:
struct Point {
int x;
int y;
};
You can initialize a structure while declaring it or later using assignment. Here's how to initialize a structure during declaration:
struct Point p1 = {10, 20};
You can also initialize a structure using dot notation:
p1.x = 30;
p1.y = 40;
Accessing structure members is done using the dot . operator. For example:
printf("x = %d, y = %d\n", p1.x, p1.y);
You can pass structures as arguments to functions. When passing a structure to a function, you generally pass it by value or by reference (using pointers).
Passing by value:
void printPoint(struct Point p) {
printf("x = %d, y = %d\n", p.x, p.y);
}
Passing by reference:
void modifyPoint(struct Point *p) {
p->x = 50;
p->y = 60;
}
You can nest structures inside other structures to create more complex data structures. Here's an example:
struct Address {
char street[50];
char city[30];
char state[20];
};
struct Employee {
char name[50];
int emp_id;
struct Address address;
};
You can create arrays of structures to store multiple records of the same structure type. For example:
struct Point points[5];
points[0] = (struct Point){1, 2};
points[1] = (struct Point){3, 4};
// ...
You can use pointers to structures to work with dynamic memory allocation and pass structures efficiently to functions. Here's an example of creating and using a pointer to a structure:
struct Point *ptr;
ptr = &p1;
printf("x = %d, y = %d\n", ptr->x, ptr->y);
typedef for StructuresYou can use typedef to create aliases for structure types, making your code more readable. For example:
typedef struct {
int hours;
int minutes;
} Time;
Time t1 = {9, 30};
Structures in C provide a powerful way to group variables of different data types under a single name. They are widely used in C programming to create complex data structures and represent real-world entities. Understanding how to declare, initialize, and manipulate structures is essential for writing efficient and organized C code. Practice with examples to reinforce your understanding and explore more advanced uses of structures in your programs.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


