C Structure

We have seen earlier how variables hold only one item. And how arrays can hold a number of items of the same datatype. But to hold a number of items each of different data type, C provides a datatype called structure.

For example, suppose you want to store data about a book. You might want to store its name (a string), its price (a float) and a number of pages in it (an int). Then you have to construct individual arrays, one for storing names, another for storing prices and still another for storing a number of pages.

The program becomes more difficult to handle as the number of items relating to the book go on increasing like date of publishing, a name of publisher etc. To solve this problem, C provides a special data type—the structure. Here is way to declare structure

Defination of Structure

  • A structure is a collection of different data types.
  • To declare structure in C, struct keyword is used.
  • Structures are used to represent a record.
  • Structure is a user defined datatype.
  • The elements of structure is called as member.
  • It is widely used to store book information, student information, employee information, product information etc.

Declaring a Structure

The general syntax to declare structure is given below:

A semicolon is must after closing brace. Only declaring structure does not reserve any space in memory.


In this statement a new structure of name book is define. A struct keyword is used to define a structure. The name, price, pages are a member of the structure. 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.

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 name, four for price and two for pages. These bytes are always in adjacent memory locations.

Declaring Structure Variable

To access the member of a structure, we can declare variables for the structure. Let’s see the example to declare structure variable. There are two ways to declare structure variable:

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

is same as…

It is the another way to declare variable at the time of defining structure.

Structure variables can also be initialized where they are declared.


  • The closing brace in the structure type declaration must be followed by a semicolon.
  • Declaring the structure does not reserve any space in memory.
  • Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined.
  • At the time of declaration of structure memory is not allocated. When structure variable is created then memory is allocated.
  • During declaration of structure, members of structure should not be initialized because memory is not allocated for members of structure.
  • The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator.

Accessing Structure Members

In arrays we can access individual elements of an array using a subscript. There are two ways to access structure members.

  • By dot . operator
  • By pointer & operator.



Array of Structures

To store data of 100 employees we would be required to use 100 different structure variables from e1 to e100, which is definitely not very convenient. A better approach would be to use an array of structures. Following program shows how to use an array of structures.


Following diagram shows memory allocation of an array of structure.

Uses of structure

A structure is mainly Database Management. To maintain data about employees, books in a library, items in a store, financial accounting transactions in a company etc.
They can be used for a variety of purposes like:

  • Changing the size of the cursor
  • Clearing the contents of the screen
  • Placing the cursor at an appropriate position on screen
  • Drawing any graphics shape on the screen
  • Receiving a key from the keyboard
  • Checking the memory size of the computer
  • Finding out the list of equipment attached to the computer
  • Formatting a floppy
  • Hiding a file from the directory
  • Displaying the directory of a disk
  • Sending the output to printer
  • Interacting with the mouse
Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.