12 Structures
12 Structures
Structures
struct structure_name
{
data_type member1;
data_type member2;
…
};
struct Date
{
Members of the
int day;
structure Date
int month;
int year;
};
• Example:
StudentInfo Student1, Student2;
Name Name
Student1 Id Gender Id Gender Student2
Dept Dept
struct student
{
int rollno;
int age;
char name[10];
float height;
}s1, s2, s3; /* Defines 3 variables of type student */
Members of a structure themselves
are not variables. i.e. rollno alone does
not have any value or meaning.
Assigning string:
strcpy(s1.name, “Rama”);
struct student
{
Assignment statement: int rollno;
int age;
s1.rollno = 1335; char name[20];
s1.age = 18; float height;
}s1;
s1.height = 5.8;
struct Student
int main ( ) {
{ int rollno;
struct Student int age;
{ } s1={20, 21};
int rollno;
int age; main ( )
}; {
Student s1={20, 21}; Student s2={21, 21};
Student s2={21, 21}; …
} …
}
b1.price);
return 0;
} CSE 1001 Department of CSE 05/23/2024 17
Structure: Example
struct Book { // definition
char title[20];
char author[15];
int pages;
float price;
};
int main( ){
struct Book b1;
printf(“Input values”);
gets(b1.title); gets(b1.author);
scanf(“%d %f”, &b1.pages, &b1.price);
//output
printf(“%s %s %d %f”, b1.title, b1.author, b1.pages,
b1.price);
return 0;
} CSE 1001 Department of CSE 05/23/2024 18