Structures

Defining and using structures to group related data together. Accessing structure members using the dot operator.


Structures in C

Introduction to Structures

A structure in C is a user-defined data type that allows you to combine different data types into a single unit. It's like a container for logically related data. This allows you to treat a collection of variables as a single entity, enhancing code organization and readability.

Defining a Structure

The struct keyword is used to define a structure. Here's the general syntax:

 struct structure_name {
                data_type member1;
                data_type member2;
                // ... more members
            }; 

For example, to define a structure for representing a point in 2D space:

 struct Point {
                int x;
                int y;
            }; 

Declaring Structure Variables

Once a structure is defined, you can declare variables of that structure type:

 struct Point p1, p2;  // Declares two variables, p1 and p2, of type 'struct Point' 

You can also initialize structure variables during declaration:

 struct Point p3 = {10, 20}; // Initializes p3 with x = 10 and y = 20 
 // another example of declaration with initialization
            struct Person {
              char name[50];
              int age;
              float salary;
            };
            struct Person person1 = {"John Doe", 30, 50000.00}; 

Accessing Structure Members

This section explains how to access and manipulate the individual members (variables) within a structure.

Using the Dot Operator (.)

The dot operator (.) is used to access individual members of a structure variable. The syntax is:

 structure_variable.member_name 

For example, to access the x and y members of the p1 variable (defined as struct Point p1;):

 p1.x = 5;  // Assigns the value 5 to the x member of p1
            p1.y = 10; // Assigns the value 10 to the y member of p1

            printf("x = %d, y = %d\n", p1.x, p1.y); // Prints the values of x and y 

Reading Structure Member Values

To read the value of a structure member, simply use the dot operator and the member name. You can then use the value in expressions, print it, or assign it to another variable.

 int x_coordinate = p1.x;  // Assigns the value of p1.x to the variable x_coordinate
            printf("The x coordinate is: %d\n", x_coordinate); // Prints the value of x_coordinate 

Modifying Structure Member Values

To modify the value of a structure member, use the dot operator to access the member and then use the assignment operator (=) to assign a new value.

 p1.x = 15; // Modifies the value of p1.x to 15
            printf("The new x coordinate is: %d\n", p1.x); // Prints the updated value 

You can also use expressions to modify member values:

 p1.y = p1.y + 5;  // Increments the value of p1.y by 5
            printf("The new y coordinate is: %d\n", p1.y); 

Example demonstrating reading and modifying structure members:

 #include <stdio.h>

            struct Student {
                char name[50];
                int roll_number;
                float marks;
            };

            int main() {
                struct Student student1;

                // Assigning values to structure members
                strcpy(student1.name, "Alice Smith");
                student1.roll_number = 123;
                student1.marks = 85.5;

                // Reading and printing the values of structure members
                printf("Student Name: %s\n", student1.name);
                printf("Roll Number: %d\n", student1.roll_number);
                printf("Marks: %.2f\n", student1.marks);

                // Modifying the marks
                student1.marks = 92.0;
                printf("Updated Marks: %.2f\n", student1.marks);

                return 0;
            }