Structures
• Structures
• Defining structures
• Accessing members
• Array of structures
• The structure in C is a user-defined data type that can be used to group items
of possibly different types into a single type. The struct keyword is used to
define the structure in the C programming language. The items in the
structure are called its member and they can be of any valid data type.
• C Structure Declaration
• The above syntax is also called a structure template or structure prototype
and no memory is allocated to the structure in the declaration.
• Consider the following details related to a student: student roll number,
student name and total marks obtained. These data can be grouped as a
structure as follows:
struct student
{
int roll_no;
char sname[30];
int total;
};
• Here student is the tag-name of the new compound data. Using this data,
structure variables and arrays can be declared.
Structure Variable Declaration with
Structure Template
• Structure variables can be declared using the structure-name of the
structure along with the keyword struct.
struct student
{
int roll_no;
char sname[30];
int total;
}x,y,z,std[50];
Structure Variable Declaration after
Structure Template
struct student
{
int roll_no;
char sname;
int total;
};
struct student x,y,z;
struct student std[50];
Access Structure Members
• We can access structure members by using the ( . ) dot operator.
Initialize Structure Members
• We can initialize structure members in 3 ways which are as
follows:
1.Using Assignment Operator.
2.Using Initializer List.
3.Using Designated Initializer List.
1. Initialization using Assignment Operator
2. Initialization using Initializer List: In this type of initialization, the values are
assigned in sequential order as they are declared in the structure template.
3. Initialization using Designated Initializer List
• Define a structure with the following three members:roll no, name and total marks of a
student. Write a C program to read and display the details of a student.
• WAP to input information about n students in a class given the following: Roll no, student
name, total marks. The program should output the marks of a specified student given their
roll no.
• Declare a structure of a student with details like roll no, student name and total marks.
Using this, declare an array with 50 elements. WAP to read details of n students and print
the list of students who have scored 75 marks and above.
• Try on your own