C Programming – Understanding Structures in C

Dealing with the data is an important feature, every programming language has to consider. Hence we are allowed to define different data types of variables to store & retrieve the data with ease. But, some times, these data types (primitives or predefined data types) are not enough to our programming need; and we need complex or our own defined data types. This is where structures comes into the picture, in C.

Structures in C, are allowed to us to group the related data, to form a complex or user-defined data types; and allowed to access or modify the data through structure variables.

Defining a Structure

struct keyword is used to define a structure in C. As, it is to group the related data fields, we should enclose them in curly braces, ‘{‘ and ‘}’. Finally structure definition ends with a semicolon ‘;’.

Here is a syntax to define a structure in C language

struct [ Structure_Name ] {
	data_type_1 member_1;
	data_type_2 member_2;
	...
	...
	...
	data_type_N member_N;
} [ structure_variable_1, structure_variable_2, ... , structure_variable_N ];

Where Structure_Name is the name of the structure which is used to define the structure variables of type Structure_Name. Note that, this is optional. You can define a structure in C, without it’s name; but, end of the structure, you need to define a structure_variable to allow to access or modify it’s members.

Defining a structure_variable at the time of defining the structure, is optional. Confused? 🙂  When we have the Structure_Name, we can define it’s variable, like below;

struct Structure_Name structure_variable_1 [, structure_variable_2, ... , structure_variable_N ];

Access structure members

Once a structure is defined, we can access it’s members through structure_variable and using a member access operator.“. Here is it looks like;

structure_variable.member_1;

Observer that, member access operator (.) is required, to access structure members. Once we have the access to it’s members; we can use them like a normal variables.

Initializing structure members

We cannot assign the structure members within the structure definition. We can only assign the members with default data, during declaring the structure_variable. Fortunately, C compiler, by default, sets the structure member variables (numeric variables) to the value 0.

During the structure_variable declaration, either we use member access operator (.) to assign the data to the specific structure members; or we can use fixed order to assign the values to the members of the structure.

Here is an example, to assign the value to the specific member;

struct Structure_Name structure_variable = { .member_7 = value, .member_2 = value };

Here is another example, where we assign values to the structure members, in the order they defined;

struct Structure_Name structure_variable = { value_1, value_2, ..., value_N };

Passing structures as function arguments

Like normal variables, we can pass structures as function arguments. When we pass them as values, whole structure data will be copied and passed to the function argument. When we pass them as a reference or pointer, only an address of the structure variable will be passed as an argument. This is an important difference, we need to remember, when we use structures as function arguments.

When we deal with large data, it is recommended to pass by reference instead of pass by value; to improve the performance.

Here is an example where we pass the structure, by value;

void function(struct Structure_Name structure_variable) {
	// function definition
}

Example where we pass the structure reference or pointer;

void function(struct Structure_Name *p) {
	// function definition
}

Functions return structures

We are allowed to return structures from functions. This is another useful feature. We can return the structure from functions, by value or by reference; same as, passing structures as arguments to the functions.

But, bit cautious when you define the structure and function together. Let’s see below example; so, it will be clear to you, what I meant;

    struct SomeStructure
    {
        // structure members here
    }
    
    SomeFunction()
    {
        // function code goes here
    }

Have you noticed any difference here? From above code, the function SomeFunction is returning the SomeStructure. Not clear? As we are not closed the definition of the SomeStructure using semicolon (;) and the function definition started with any return type, compiler will treat it as function returning a structure.

To make it clear, and avoid confusion; it is better to define the structure & function definitions like below;

#include <stdio.h>

int main()
{
    struct Vehicle
    {
        char name[20];
    };
    
    struct Vehicle JustReturnTheStructure()
    {
        struct Vehicle car = {"Car"};
        
        return car;
    }
    
    printf("%s", JustReturnTheStructure().name);

    return 0;
}

Working Example

Let’s put all together and here is our working example;

// Malin

One Comment

Add a Comment

Your email address will not be published.