Structures In C

Structures In C

INTRODUCTION

A structure is a user-defined datatype that groups related variables of different data types. It is a collection of variables under a single name. Data can be from different related fields which when combined makes it meaningful e.g. Gathering the data of 6 students in a class(their name, age and other data). Structure will eliminate the use of different arrays to group the data gotten, since each student will have related fields like their name, age, class etc.

DECLARATION

Structure declaration includes the keyword struct, a structure tag for referencing the structure, and curly braces {} ended with a semi-colon :, within the structure we have a list of variables called "members".

struct student
{
    char name[40];
    int age;
    float height;
};

struct student s1;

The struct statement defines a new data type name student with 3 members (name, age, height). A variable s1 is then declared using the keyword struct and the structure name student which can be used to store data according to the datatype defined in struct student. Structures can be of any datatype and even other structures. A structure is also called a composite or aggregate data type.

INITIALIZING

There are several ways to initialize a structure variable

struct student s1 = {"techWhiz", 18, 5.0};
struct student s2 = {"codeboy", 20, 4.3};
struct student s3 = {"tech"};//name is initialized with value but the other datatype is 0

In this method, the variable is initialized the same way it is defined in the structure student. The order of placement must match i.e. name(char), age(int), and height(float). Initializing the variable with a single datatype will store the value given to the first datatype and initialize the rest to NULL or 0.

s1 = (struct student) {"techWhiz", 18, 5.0};

This method is called typecasting.

struct student s1 = {.age = 18, .height = 5.0, .name = "techWhiz"};

In this method, the order of placement is ignored by a dot . and the member name is initialized with the datatype of the member.

struct student s1;
s1.name = "techWhiz";
s1.age = 18;
s1.height = 5.0;

Similar to the previous code. An array of structures is simply an array in which each element is a structure of the same type. The referencing and subscripting of these arrays (also called structure arrays) follow the same rules as simple arrays.

ARRAY OF STRUCTURE

An array of structure refers to an array of structure variables. It is simply an array in the elements in the array is a structure of the same datatype. The referencing and subscripting of these arrays (also called structure arrays) follow the same rules as simple arrays.

struct student s[3]; //creates 3 ibjects of a same structure in an array S

#include <stdio.h>

struct student
{
    char name[20];
    int age;
    float height;
}; //student structure 

int main(void)
{
    int i; // counter 

    struct student s[3] = {
        {"techWhiz", 18, 5.0},
        {"codeboy", 15, 4.3},
        {"tech", 17, 4.8}
    }; //array structure 

    //prints the name, age and height of each elements in the array
    for (i = 0; i < 3; i++)
    {
        printf("Name: %s\n", s[i].name);
        printf("Age: %d\n", s[i].age);
        printf("Height: %.1f\n", s[i].height);
        putchar('\n');
    }
    return (0);
}

This program is defining a structure called student which has three members: name, age, and height. It then declares an array of three student structures called s. The program initializes the array with the values of the members of each structure. Finally, it prints the name, age and height of each element in the array using a for loop.

POINTERS TO STRUCTURES

Pointers are very useful in almost every program in C. Pointers can also be used to access the members of a structure datatype.

struct struct_name * struct_ptr - defines a pointer to the struct_name structure
struct_ptr = &struct_var - stores the address of struct_var in pointer 
struct_ptr->struct_member - access the value of the structure member struct_member
(*struct_ptr).age is the same as struct_ptr->struct_member
#include <stdio.h>

struct student
{
    char name[20];
    int age;
    float height;
};

int main(void)
{
    struct student s = {"techWhiz", 18, 5.0};

    struct student *ptr;
    ptr = &s;

    printf("%s\n", ptr->name);
    printf("%d\n", (*ptr).age);
    printf("%.1f\n", (*ptr).height);

    return (0);
}

TYPEDEF

Typedef is commonly used with structures because it eliminates the need to use the keyword struct when declaring variables.

typedef struct
{
        char name[20];
        int age;
        float height;
}student;

student s1; //declares a variable s1

In summary, structures as user-defined datatypes making it easy for grouping related data. Other complex data types such as dates, time etc can be created through structures that are not present in the language. Using structures is a good practice whenever you are managing a large amount of data that needs to be organized properly.

ย