Structures in C

In C, a structure is a composite data type that allows you to group variables of different data types under a single name.

Structures in C

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.

Declaring Structures

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;
};

Initializing Structures

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;

Acessing Structure Members

Accessing structure members is done using the dot . operator. For example:

printf("x = %d, y = %d\n", p1.x, p1.y);

Structures as Function Arguments

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;
}

Nested Structures

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;
};

Arrays of Structures

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};
// ...

Pointers to Structures

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 Structures

You 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.