Structure in c programming

In this tutorial, you will learn about structure in c programming and how they are used in c programs.

C programming structures

Similar to array structures are used to represent a collection of data items but of similar and different types using the single name. Structure is a user defined datatype where we have to design and declare a data structure before the variable of that type are declared and used. So it can be said that structures help to organize complex data in a meaningful way.

General format of structure

Here the keyword struct declares a structure that will hold the datafields named members. ‘tag_name’ is a name of the structure that we have declared. Inside the structure, we can use any kind of datatypes like int, float, char and others.

Things to remember

The structure template must terminate with a semicolon.

That tag_name can be used to declare structure variable of its type later in the program

Declaring structure variable

Now that we have defined and designed structure, next step is to declare a structure variable in programs. Here is how it is done:

struct tag_name variable1, variable2;

Here variable1 and variable2 are variables declared of type struct tag_name. Each one of these variables will hold members defined inside the structure.

Here product1 and product2 are variable which will hold members product_name and product_number.
NOTE: Remember that members inside the structure are not variables and they do not occupy memory until they are associated with structure variables such as product1 and product2.

Here name1, name2, and name3 are structure variables representing members but such declaration has no tag_name.
This method is not recommended because, without a tagname, we cannot use structure variable of its type for future declarations.

Accessing structure members

As we already know members themselves are not variables and they don’t hold any meaning and memory unless they are associated with a structure variable. So to access those members member operator ‘.’ is used which is also called dot operator.
For example: