Loading...
Loading...

C Structures Tutorial

Structures in C allow you to group variables of different types into a single entity. This tutorial will explain the basics of structures, their declaration, initialization, and how to use pointers with them.

1. What is a Structure?

A structure is a user-defined data type in C that allows you to group different data types into a single unit. Each element in a structure is called a member or field.

2. Declaring a Structure

Structures are declared using the struct keyword. Here's how you declare a structure:

#include <stdio.h>

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

int main() {
    struct Person person1;
    person1.age = 30;
    printf("Age: %d\\n", person1.age);
    return 0;
}

Here, the Person structure has two members: name and age.

3. Initializing Structures

You can initialize a structure at the time of declaration by providing values for its members:

#include <stdio.h>

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

int main() {
    struct Person person1 = {"Alice", 25};  // Initialize structure
    printf("Name: %s, Age: %d\\n", person1.name, person1.age);
    return 0;
}

The person1 structure is initialized with the name Alice and age 25.

4. Accessing Structure Members

To access a structure's member, use the dot operator (.):

#include <stdio.h>

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

int main() {
    struct Person person1 = {"Bob", 30};
    printf("Name: %s, Age: %d\\n", person1.name, person1.age);
    return 0;
}

Here, person1.name and person1.age are used to access the values of the structure's members.

5. Pointers to Structures

Structures can also be pointed to by pointers. To access members of a structure using a pointer, use the arrow operator (->):

#include <stdio.h>

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

int main() {
    struct Person person1 = {"Charlie", 35};
    struct Person *ptr = &person1;  // Pointer to structure
    printf("Name: %s, Age: %d\\n", ptr->name, ptr->age);  // Using arrow operator
    return 0;
}

The arrow operator (ptr->name) accesses the members of the structure through the pointer.

6. Nested Structures

You can also have structures within other structures. This is known as a nested structure:

#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Person {
    char name[50];
    struct Date birthDate;  // Nested structure
};

int main() {
    struct Person person1 = {"David", {12, 6, 1990}};
    printf("Name: %s, Birth Date: %d/%d/%d\\n", person1.name, person1.birthDate.day, person1.birthDate.month, person1.birthDate.year);
    return 0;
}

In this example, the Person structure contains a Date structure as a member.

7. Conclusion

Structures are a powerful feature in C programming, allowing you to group different data types together. Understanding how to declare, initialize, and manipulate structures will help you manage complex data more efficiently.

0 Interaction
1.2K Views
Views
13 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home