Structure is a user-defined data type, which allows you to combine data items of different kinds. To define a structure use struct keyword.
Syntax: Define structure
struct structure_name { data_type member1; data_type member2; data_type member3; data_type member4; . . data_type memberN; };
Way 1: Declare structure object at the time of defining structure.
struct student{ int rollno; char name[20]; }s1,s2;
Way 2: Declare structure object in main() function
void main() { struct student s1, s2; }
If number of object is fixed use first way, it saves your code. If number of object is not fixed use second way. It provides you flexibility to declare the structure variable many times.
Example 1: Define a student structure, input a student record and display it.
struct student { int rollno; char name[20]; }s1; void main() { struct student s2; clrscr(); printf("Enter 1st Student - Rollno, Name: "); scanf("%d",&s1.rollno); flushall(); gets(s1.name); printf("Enter 2nd Student - Rollno, Name: "); scanf("%d",&s2.rollno); flushall(); gets(s2.name); printf("\nYour Rollno = %d",s1.rollno); printf("\nYour Name = %s",s1.name); printf("\n\nYour Rollno = %d",s2.rollno); printf("\nYour Name = %s",s2.name); getch(); }
We can create array of structure means it is possible to create array of structure's object in the same way as we have created array for primitive data type. The benefit of creating array of structure is that we can store multiple records of similar type.
Example 2: Input record of 5 students and display it using array of structure.
struct student { int rollno; char name[20]; int age; }; void main() { struct student s[10]; //structure object array int i; clrscr(); for(i=0;i<5;i++) //input 5 records { printf("Enter rollno, name of %d student", i+1); scanf("%d",&s[i].rollno); flushall(); gets(s[i].name); printf("Enter age"); scanf("%d",&s[i].age); } for(i=0;i<5;i++) //display 5 records { printf("\n\nYour rollno = %d",s[i].rollno); printf("\nYour name = %s",s[i].name); printf("\nYour Age = %d",s[i].age); } getch(); }
Example 3: Input student record and display it using object pointer to structure.
struct student { int rollno; char name[20]; int age; }; void main() { struct student *s; int i; clrscr(); printf("Enter rollno, name of student"); scanf("%d",&s->rollno); flushall(); gets(s->name); printf("Enter age"); scanf("%d",&s->age); printf("\n\nYour rollno = %d",s->rollno); printf("\nYour name = %s",s->name); printf("\nYour Age = %d",s->age); getch(); }
When we create a structure inside another structure it is known as nested structure. In C language, we can create nested structure by using two ways:
We can create 2 separate structures, but the object of dependent structure should be declare inside the main structure as a member.
Example 4: Nested separate structure
#include<stdio.h> #include<conio.h> struct student { int rollno; char name[20]; }; struct marks { int hindi, english, math; struct student s1; //creating object of student structure }m1; void main() { int total; float per; clrscr(); printf("Enter Rollno, Name, Hindi, English, Math marks: "); scanf("%d%s%d%d%d",&m1.s1.rollno, &m1.s1.name, &m1.hindi, &m1.english, &m1.math); printf("\nRoll no = %d\nName = %s\nHindi = %d\nEnglish = %d\nMath = %d",m1.s1.rollno, m1.s1.name, m1.hindi, m1.english, m1.math); total = m1.hindi + m1.english + m1.math; per = total/3.0; printf("\nTotal = %d",total); printf("\nPercentage = %f",per); getch(); }
We can access the member of nested structure by Outer_Structure.Nested_Structure.member
We can define one structure within another structure. The dependent structure can't be used in many structures.
Example 5: Nested embedded structure
#include<stdio.h> #include<conio.h> struct marks { int hindi, english, math; struct student //declaring structure inside another structure { int rollno; char name[20]; }s1; }m1; void main() { int total; float per; clrscr(); printf("Enter Rollno, Name, Hindi, English, Math marks: "); scanf("%d%s%d%d%d",&m1.s1.rollno, &m1.s1.name, &m1.hindi, &m1.english, &m1.math); printf("\nRoll no = %d\nName = %s\nHindi = %d\nEnglish = %d\nMath = %d",m1.s1.rollno, m1.s1.name, m1.hindi, m1.english, m1.math); total = m1.hindi + m1.english + m1.math; per = total/3.0; printf("\nTotal = %d",total); printf("\nPercentage = %f",per); getch(); }
You can pass a structure as a function argument in very similar way as you pass any other variables.
Example 6: Structure as function argument
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books book ); 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, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info */ printBook( Book1 ); /* Print Book2 info */ printBook( Book2 ); return 0; } void printBook( struct Books book ) { printf( "Book title : %s\n", book.title); printf( "Book author : %s\n", book.author); printf( "Book subject : %s\n", book.subject); printf( "Book book_id : %d\n", book.book_id); }
Example 7: Pointer structure as function argument
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books *book ); void 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, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info by passing address of Book1 */ printBook(&Book1 ); /* print Book2 info by passing address of Book2 */ printBook(&Book2 ); getch(); } void printBook(struct Books *book ) { printf("Book title : %s\n", book->title); printf("Book author : %s\n", book->author); printf("Book subject : %s\n", book->subject); printf("Book book_id : %d\n", book->book_id); }
Unions are the same as a structures but the difference is that structure will take separate memory location for the variables declare in it whereas union will share memory space for all the variables. Union may affect the original value but structure will not affect the values.
Example 8: Nested embedded structure
#include<stdio.h> #include<conio.h> struct sstudent { int rollno; //2 bytes char name[20]; //20 bytes int age; //2 bytes double per; //8 bytes }; union ustudent { int rollno; //2 bytes char name[20]; //20 bytes int age; //2 bytes double per; //8 bytes }; void main() { struct sstudent s; union ustudent u; clrscr(); printf("Size of Structure = %d",sizeof(s)); printf("Size of Union = %d",sizeof(u)); getch(); }
S.No | Structure | Union |
---|---|---|
Each member assign unique storage area. | All the member share same storage area. | |
Each member can assign different values. | Each member need not be assigned a different value. |
C Language Feedback, Questions, Suggestions, Discussion.