Home >>C Tutorial >C Structure

Structure in C

Structures in C

As a fact, we know that types of variable that can hold several data items of the same type are defined by the arrays. Structures in C language works kind of similar to the array as these structures are the user defined data types in the C language and data items of various kinds are permitted by them.

The use of the structure in the C language is mainly to represent a record. Let’s assume you want to keep a track of your students in a class then you will need these following attributes of each student:

  • Name
  • Roll number
  • Class
  • Blood Group

Let's understand the process of defining a structure in C

Struct statements are must to use when you are trying to define a structure in C. the struct statement is known to define a new data type having more than one number.

syntax of the struct statement in the C language:

struct [structure tag] { 

   member definition;

   member definition;


   member definition;

} [one or more structure variables];

Each member in the struct statement is a normal variable definition like int i; or float f; or some other valid variable definition and the struct tag is optional.

One or more than one struct variable can be specified, just before the final semicolon and at the end of structure’s definition but it is meant to be optional. Following is the way to declare the student structure:

struct Students { 

   char  name[50]; 

   char  roll number[50]; 

   char  class[100]; 

   int   student id; 

} student;

Let's get to know the accessing the structure members in the C language

The member access operator (.) is used in order to access any member of the structure. Between the structure variable name and the structure member that a user want to access, the member access operator is generally coded as a period. In order to define the variables of the structure type, the struct keyword is generally used.

Here is an example that will define it better for the users to understand:

	Example will display here

Let's understand the structures as function arguments in the C language

Structures can be passed as a function argument just in the same way as a variable or a pointer is passed in the C language.

	Example will display here

Let's understand the pointers to structures in C language

Pointer to Structures can be defined just in the same way as any other variable or a pointer is defined in the C language.

struct Student *struct_pointer;

Now that the structure is defined, the address of the structure variable can be stored as in the above defined pointer variable. You have to place the ‘&’ operator just before the structure’s name in order to find the address of the structure variable.

struct_pointer = &Student1;

The members of a structure can be accessed just by using a pointer to that structure. Follow the below mentioned way: struct_pointer->name;

	Example will display here

Bit fields in the C language

The packing of the data in a structure is permitted by the bit fields. The special use of bit fields lies when the storage of the memory or data is at a premium. Here are the most typical example of them:

  • when several objects are packed in to a machine word.
  • When external formats are needed to read.

It is allowed to do this in a structure definition by the C language just by placing the: bit length after a variable.

	Example will display here

The bit fields are compactly packed to the brim by the C language automatically only if the maximum length of the field is equal to or less than the integer word length. Apart from this case, memory overlap of the fields are allowed by the compilers while others are known to store the next field in the next word.