Structure
C arrays allow you to define type of variables that can hold several data
items of the same kind but structure is another user defined data type
available in C programming, which allows you to combine data items of
different kinds.
Structures are used to represent a record, Suppose you want to keep track
of your books in a library. You might want to track the following attributes
about each book:
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member for your
program. The format of the struct statement is this:
struct tag
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
};
OR
struct <structure_name>
{
structure_Element1;
structure_Element2;
...
};
The structure tag is optional and each member definition is a normal
variable definition, such as int i; or float f; or any other valid variable
definition. At the end of the structure's definition, before the final
semicolon, you can specify one or more structure variables but it is
optional. Here is the way you would declare the Book structure:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Structure Declarations
A "structure declaration" names a type and specifies a sequence of variable
values (called "members" or "fields" of the structure) that can have
different types. An optional identifier, called a "tag" gives the name of the
structure type and can be used in subsequent references to the structure
type. A variable of that structure type holds the entire sequence defined by
that type. Structures in C are similar to the types known as "records" in
other languages.
Syntax
Way 1 : Immediately after Structure Template
struct date
{
int date;
char month[20];
int year;
}today;
// 'today' is name of Structure variable
Way 2 : Declare Variables using struct Keyword
struct date
{
int date;
char month[20];
int year;
};
struct date today;
where “date” is name of structure and “today” is name of variable.
Way 3 : Declaring Multiple Structure Variables
struct Book
{
int pages;
char name[20];
int year;
}book1,book2,book3;
We can declare multiple variables separated by comma directly after
closing curly.
C Structure Initialization
When we declare a structure, memory is not allocated for un-initialized
variable. So We need to initialize structure variable to allocate some memory to
the structure.
Syntax:- Declare and Initialize
struct student
{
char name[20];
int roll;
float marks;
}std1 = { "Pritesh",67,78.3 }; //Initialize structure Veriable
Initializing inside main
struct student
{
int mark1;
int mark2;
int mark3;
};
void main()
{
struct student s1 = {89,54,65};
};
When we declare a structure then memory won’t be allocated for the structure.
i.e only writing below declaration statement will never allocate memory
struct student
{
int mark1;
int mark2;
};
We need to initialize structure variable to allocate some memory to the
structure.
struct student s1 = {89,54,65};
Accessing members of a structure
There are two types of operators used for accessing members of a structure.
1. Member operator(.)
2. Structure pointer operator(->)
To access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure variable
name and the structure member that we wish to access. You would use struct
keyword to define variables of structure type. Following is the example to
explain usage of structure:
Any member of a structure can be accessed as:
structure_variable_name . member_name
Suppose, we want to access salary for variable p2. Then, it can be accessed as:
p2.salary
/*WAP to enter student name ,roll number , marks and display
information*/
#include <stdio.h>
#include <string.h>
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
Output
Enter information of students:
Enter name: Adele
Enter roll number: 21
Enter marks: 334.5
Displaying Information
name: Adele
Roll: 21
Marks: 334.50
Array of Structure :
Structure is used to store the information of One particular object but if
we need to store such 100 objects then Array of Structure is used.
example:
struct inventory
{
Char name;
Int cost;
Int quantity price;
};
struct inventory table[4];
which defines an array with four elements, each of which is of type
struct inventory, i.e. each is an inventory structure.
We can think of such a data structure as a tabular representation of our data
base of parts inventory with each row representing a part, and each column
representing information about that part, i.e. the name, cost, and
quantity , as shown in Figure
This is very similar to a two dimensional array, except that in an array,
all data items must be of the same type, where an array of structures
consists of columns, each of which may be of a distinct data type.
C initializing array of structure
Way 1: Initializing After Declaring Structure Array :
struct Book
{
char bname[20];
int pages;
char author[20];
float price;
}b1[3] = { {"Let us C",700,"YPK",300.00},
{"Wings of Fire",500,"APJ Abdul Kalam",350.00},
{"Complete C",1200,"Herbt Schildt",450.00}
};
Explanation :
As soon as after declaration of structure we initialize structure with the pre-
defined values. For each structure variable we specify set of values in curly
braces. Suppose we have 3 Array Elements then we have to initialize each
array element individually and all individual sets are combined to form
single set.
Way 2 : Initializing in Main
struct Book
{
char bname[20];
int pages;
char author[20];
float price;
};
void main()
{
struct Book b1[3] = { {"Let us C",700,"YPK",300.00},
{"Wings of Fire",500,"Abdul Kalam",350.00},
{"Complete C",1200,"Herbt Schildt",450.00}
};
}
Uses of C structures:
1. C Structures can be used to store huge data. Structures act as a
database.
2. C Structures can be used to send data to the printer.
3. C Structures can interact with keyboard and mouse to store the data.
4. C Structures can be used in drawing and floppy formatting.
5. C Structures can be used to clear output screen contents.
6. C Structures can be used to check computer’s memory size etc.