C Programming

Structures in C

In C, a structure is a user-defined variable used to store a collection of variables under a single entity. Let us use a simple analogy to explain structures implementation and usefulness in C.

Suppose we want to store information about users using a specific service. Such information can include the username, email, address, service mode, and such. To store such information, we can go about creating each attribute as a standalone variable. However, when we have ten plus users, the code can spiral out of control and become very difficult and tiresome to read.

To solve this, we can create a structure. Inside the structure, we can store all the attributes shared by all the users and then add unique variables for each user.

Let us take a look at various examples to see how to implement this.

How to Define a Structure in C

To define a structure in C, we use the struct keyword followed by the structure’s name. After the name, we have a pair of curly braces where we add the members.

Consider the syntax below:

struct struct_name
{
    /* data */
    type member_name;
    type member_name2;
    type member_name3;

    ...
    type member_nameN;
};

The structure’s name can be anything you want as long as it adheres to the naming convention of the C programming language.

We can implement an example structure of the user analogy as:

struct users
{
    char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
};

How to Create Structure Variables

There are two main ways to create structure variables. The first one is to declare them like normal variables, and the other is to set them using curly braces.

The example below shows how to declare structure variables as standard C variables.

struct users
{
    char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
};

int main(int argc, char const *argv[])
{
    struct users user1, user2, user3
    return 0;
}

The other method of creating structure variables is as shown below:

struct users
{
    char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
} user1, user2, user3;

In this example, we create them during structure declaration.

Structure Member Init

You cannot initialize the structure members during creation as there is no memory allocated for the type.

To initialize the members of a structure, you use the curly braces as shown below:

struct users
{
    char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
};
int main(int argc, char const *argv[])
{
    struct users user1 = {"myusername", "[email protected]", 35, true}
    return 0;
}

Access Structure Members

To access the members of a structure, we use the dot operator, starting with the structure name, a dot, and the name of the member.

struct users
{
    char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
};
int main(int argc, char const *argv[])
{
    struct users user1 = {"myusername", "[email protected]", 35, true}
    user1.email = "[email protected]"
    return 0;
}

In this example, we access the members of user1.

Typedef Keyword

We use the typedef keyword to create an alias for the data types, making the code more readable.

For example:

typedef struct users
{
        char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
} u;
int main(int argc, char const *argv[])
{
    u user1 = {"myusername", "[email protected]", 35, true}
    user1.registered = false
    return 0;
}

In the example above, we created an alias “u” for the “users” structure. Therefore, we do not need to call struct users every time. We can use “u” as defined above.

Structure Pointers

You can also have a pointer to a structure. Doing this allows you to access the members using the -> operator.

For example:

typedef struct users
{
        char username[20];
    char email[225];
    char address[50];
    int age;
    bool registered;
} u;
int main(int argc, char const *argv[])
{
    u user1 = {"myusername", "[email protected]", 35, true}
   
    // pointer to another structure
    u *user_ptr = &user1
    user_ptr -> username = "ptrusername"
    return 0;
}

Conclusion

This guide covers the basics of working with structures in the C programming language.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list