Structures in C: Usage and Examples
Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user-defined data type available in C that allows combining data items of different kinds.
Structure helps to construct a complex data type in a more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store a collection of any type of data.
Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.
Structure Definition in C
struct keyword is used to define a structure. struct defines a new data type which is a collection of different types of data.
struct structure_name
{
//declaration of different data types
};
The closing braces in the structure type declaration must be followed by a semicolon(;).
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Note: Don't forget the semicolon }; in the ending line.
We can create the structure for a person as mentioned above as:
struct person
{
char name[50];
int citNo;
float salary;
};
This declaration above creates the derived data type struct person.
Structure variable declaration
When a structure is defined, it creates a user-defined type but, no storage or memory is allocated.
Declaring Structure variables separately
For the above structure of a person, variable can be declared as:
struct person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct person person1, person2, person3[20];
return 0;
}
Declaring Structure Variables with Structure definition
Another way of creating a structure variable is:
struct person
{
char name[50];
int citNo;
float salary;
} person1, person2, person3[20];
In both cases, two variables person1, person2 and an array person3 having 20 elements of type struct person are created.
Accessing members of a structure
There are two types of operators used for accessing members of a structure.
- Member operator(.)
- Structure pointer operator(->)
Any member of a structure can be accessed as:
structure_variable_name.member_name
Suppose, we want to access salary for variable person2. Then, it can be accessed as:
person2.salary
Structure Initialization
Like any other data type, structure variable can also be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
or
struct patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;
Example Program
Program
#include <stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Rambo Azmi");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
Output
employee 1 id : 101
employee 1 name : Rambo Azmi
Press any key to continue . . .
Program
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Rambo Azmi");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Snowfish");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
Output
Book 1 title : C Programming
Book 1 author : Rambo Azmi
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Snowfish
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Press any key to continue . . .