CodeToLive

Structures in C

Structures in C are user-defined data types that allow you to group variables of different data types under a single name. They are useful for representing complex data structures like records, objects, or entities.

Defining a Structure

To define a structure, use the struct keyword followed by the structure name and a block of member variables.


#include <stdio.h>

// Define a structure
struct Student {
    char name[50];
    int age;
    float marks;
};

int main() {
    // Declare a structure variable
    struct Student student1;

    // Access and assign values to structure members
    strcpy(student1.name, "John Doe");
    student1.age = 20;
    student1.marks = 85.5;

    // Print structure members
    printf("Name: %s\n", student1.name);
    printf("Age: %d\n", student1.age);
    printf("Marks: %.2f\n", student1.marks);

    return 0;
}
      

Accessing Structure Members

Structure members are accessed using the dot (.) operator.


#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1;
    p1.x = 10;
    p1.y = 20;

    printf("Coordinates: (%d, %d)\n", p1.x, p1.y);
    return 0;
}
      

Array of Structures

You can create an array of structures to store multiple records.


#include <stdio.h>

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

int main() {
    struct Student students[3];

    // Assign values to the array of structures
    for (int i = 0; i < 3; i++) {
        printf("Enter name, age, and marks for student %d: ", i + 1);
        scanf("%s %d %f", students[i].name, &students[i].age, &students[i].marks);
    }

    // Print the array of structures
    for (int i = 0; i < 3; i++) {
        printf("Student %d: Name = %s, Age = %d, Marks = %.2f\n", i + 1, students[i].name, students[i].age, students[i].marks);
    }

    return 0;
}
      

Nested Structures

Structures can contain other structures as members, allowing you to create complex data types.


#include <stdio.h>

struct Address {
    char city[50];
    char state[50];
};

struct Employee {
    char name[50];
    int age;
    struct Address address;
};

int main() {
    struct Employee emp1;

    strcpy(emp1.name, "Alice");
    emp1.age = 30;
    strcpy(emp1.address.city, "New York");
    strcpy(emp1.address.state, "NY");

    printf("Employee Name: %s\n", emp1.name);
    printf("Employee Age: %d\n", emp1.age);
    printf("Employee City: %s\n", emp1.address.city);
    printf("Employee State: %s\n", emp1.address.state);

    return 0;
}
      

Pointers to Structures

You can use pointers to structures to access structure members using the arrow (->) operator.


#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1 = {10, 20};
    struct Point *ptr = &p1;

    printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y);
    return 0;
}
      

Typedef with Structures

The typedef keyword can be used to create an alias for a structure, making it easier to declare variables.


#include <stdio.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person p1;
    strcpy(p1.name, "Bob");
    p1.age = 25;

    printf("Name: %s\n", p1.name);
    printf("Age: %d\n", p1.age);

    return 0;
}
      
Back to Tutorials