Declaring Structures, Trick #1

I refer to structures as “multi-variables” in my books and courses. Like a mini-database, they hold different data types and values, all bundled into a single unity. Structures form the basis of important programming concepts such as a linked list. Further, you can use structures to cheat and return multiple values from a function. As much as I dislike admitting it, structures are fun.

That’s enough joy for today.

Structures involve a bit more setup than other variables because you must define their contents, or members, then use this definition to create structure variables. Finally, you fill the structure variable’s members with values. Here you go:

/* define the structure */
struct human {
    char name[32];
    int age;
    float iq;
};

/* declare a structure variable */
struct human me;

/* fill the structure variable */
strcpy(me.name,"Dan Gookin");
me.age = 22;
me.iq = 500.5;

These steps require more statements than you would use to assign values to single variables, but the statements can be combined. In fact, most programmers declare a structure and its variables in the same statement:

struct human {
    char name[32];
    int age;
    float iq;
} me;

Multiple variables can also be declared:

struct human {
    char name[32];
    int age;
    float iq;
} me, you, him, her;

C coders also use typedef with a structure to save typing molecules, I suppose. I don’t typedef structures as it can lead to trouble if you overburden the definition by trying to do too much at once.

As with other variables, you can both declare a structure variable and assigning values in a single statement:

struct human me = { "Dan Gookin", 22, 500.5 };

Ultimately, you can combine the structure’s definition, variable declaration, and assignment, all at once:

struct human {
    char name[32];
    int age;
    float iq;
} me = { "Dan Gookin", 22, 500.5 };

You may already know this trick. What you may not know, however, is that the structure members need not be assigned in sequence. Honestly, I can’t think of a necessary reason not to declare them in sequence, but if you do, here is the format:

struct human you = {
    .iq = 89.6,
    .name = "User",
    .age = 99
};

The structure members are listed with a dot operator prefix, then the assignment made. In this format, unlike the earlier example, the members need not be assigned in a specific order. Here’s the obligatory test program:

2021_10_30-Lesson.c

#include <stdio.h>

int main()
{
    /* define the structure */
    struct human {
        char name[32];
        int age;
        float iq;
    };

    /* declare a variable and set values */
    struct human you = {
        .iq = 189.6,
        .name = "See Programmer",
        .age = 29
    };

    printf("You are %s, age %d, IQ %.1f\n",
            you.name,
            you.age,
            you.iq
          );

    return(0);
}

The human structure definition and the you variable declaration statements can be combined, of course. Yet, my point is to show that members can be assigned in any order when the dot operator is used, as shown above. Here is the sample run:

You are See Programmer, age 29, IQ 189.6

In next week’s Lesson, I cover another bizarre aspect of declaring structure variables, one that I find truly weird.

Leave a Reply