Structures in C Language

Structure in C language is a user-defined datatype that allows you to hold different type of elements.

Each element of a structure is called a member.

It is widely used to store student information, employee information, product information, book information etc.

Difference between arrays and structure

An array is a collection of related data elements of the same type. The structure can have elements of different types.

Defining Structure

1

2

3

4

5

6

7

8

9

10

11

//the struct keyword is used to

//define structure

structstructure_name

{

data_type member1;

data_type member2;

.

.

.

data_type memberN;

};

Important Points

The closing braces } in the structure type declaration must be followed by a semicolon (;).

Usually, structure type declaration appears at the top of the source code file, before any variables or functions are defined.

Example of Declaring Structure

You can store data about a book. Like data related to name(string), price(float) and pages(integer).

Let’s see the example to define a structure for books in C.

1

2

3

4

5

6

structbook

{

charname[50];

floatprice;

intpages;

};

struct – is a keyword.

book – is the tag name of the structure.

name, price, pages – are the members or fields of the structure.

This statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages.

Declaring structure variable

Once the new structure data type has been defined one or more variables can be declared to be of that type.

We can declare variable for the structure, so that we can access the member of structure easily. There are two ways to declare structure variable:

By struct keyword within main() function

By declaring a variable at the time of defining the structure.

1st way

Let’s see the example to declare structure variable by struct keyword. It should be declared within the main function.

1

2

3

4

5

6

structbook

{

charname[50];

floatprice;

intpages;

};

Now write given code inside the main() function.

1

structbook b1,b2;

This statement sets aside space in memory. It makes available space to hold all the elements in the structure—in this case, 7 bytes—one for the name, four for the price and two for pages. These bytes are always in adjacent memory locations.

2nd way

Let’s see another way to declare a variable at the time of defining the structure.

1

2

3

4

5

6

structbook

{

charname[50];

floatprice;

intpages;

}b1,b2;

Which approach is good

But if no. of variable are not fixed, use 1st approach. It provides you the flexibility to declare the structure variable many times.

If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in main() function.

Accessing Structure Elements

There are two ways to access structure members:

By dot (.) (member or dot operator)

By -> (structure pointer operator)

Having declared the structure type and the structure variables, let us see how the elements of the structure can be accessed.

In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator. So to refer to pages of the structure defined in our sample program we have to use,

1

b1.pages

Similarly, to refer to price we would use,

1

b1.price

Note that before the dot (.) there must always be a structure variable and after the dot (.) there must always be a structure element.