Structure is a “collection of data items of different datatype grouped together under a single name.”
or
structure is another user defined data type available in C that allows 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
- We can define structure any where in the Program.
- To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member.
- The format of the struct statement is as follows:-
- 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;
Need of Structure
- Till, now we have to stored value in variable and array.
- Variables stored single value at a time. And array stored multiple value but all of
- same datatype.
- But in real life, there are situations where we need to store data item but all data
- item datatypes are not same.
- Suppose Book is a collection of data item such as title, author, price.
- All data value of books are not of same type.
- In order to handle such situations, c provides a datatype, called structures.
Declare Structure Members
- In previous section we only discuss format of the structure, but we still did not declare any variables which can store value.
- so, we need to declare variable.
- Three way to declare variables
- Structure-variable declaration in structure template.
- Structure-variable declaration any where in the program.
- Array of structures.
- Here b1,b2 and b3 are the three variables of structure type-book.
- If we need the detail of 100 books, then it is difficult to declare 100 variables using above two method, problem is solved by array declaration of structure variables.
- Accessing structure member through ‘.’ Operator used with structure variable.
Accessing Structure Members
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 the keyword struct to define variables of structure type. The following example shows how to use a structure in a 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, "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 */
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 : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Wap to accept name, address and phone number of student using structure and print the same. (IMP)
# include<stdio.h>
struct student;
struct student
{
char name[80];
char address[200];
int phone;
};
void main()
{ struct student s;
printf(“enter student name”);
gets(s.name);
printf(“enter address”);
gets(s.address);
printf(“enter phone number”);
scanf(“%d”,s.phone);
Structure declaration
Structure definition
Structure variable declarationprintf(“%s”,s.name);
printf(“%s”,s.address);
printf(“%d”,s.phone);
}
Output:
Enter student name xyz
Enter address surat
Enter phone number 1234567890
Xyz
Surat
1234567890
Wap to calculate average mark of 2 student where
student has three subject. (IMP)
#include<stdio.h>
#include<conio.h>
struct student
{ char n[80];
int m1,m2,m3;
float avg;
};
void main()
{
struct student s1,s2;
printf(“enter name of student”);
gets(s1.n);
printf(“enter mark of three subject”);
scanf(“%d %d %d”,&s1.m1,&s1.m2,&s1.m3);
s1.avg=(s1.m1+s1.m2+s1.m3)/3;printf(“enter name of student”);
gets(s2.n);
printf(“enter mark of three subject”);
scanf(“%d %d %d”,&s2.m1,&s2.m2,&s2.m3);
s2.avg=(s2.m1+s2.m2+s2.m3)/3;
printf(“name of student %s”,s1.n);
printf(“marks of student %d %d %d”,s1.m1,s1.m2,s1.m3);
printf(“average mark %f”,s1.avg);
printf(“name of student %s”,s2.n);
printf(“marks of student %d %d %d”,s2.m1,s2.m2,s2.m3);
printf(“average mark %f”,s2.avg);
getch();
}
Output:
Enter student name xyz
Enter mark of three subject 99
90
91
Enter student name pqr
Enter mark of three subject 87
82 80
Name of the student xyz
Mark of three subjects 99 90
91
Average of the mark 93
Name of the student pqr
Mark of three subjects 87 82
80
Average of the mark 83
Structure Initialization
Struct book
{
char title[15];
char author[10];
int pages;
float price;
} b1={“cpu”, “xyz”,300,200.50};
Printf(“name of title %s”,b1.title);
Struct book
{
char title[15];
char author[10];
int pages;
float price;
}
;
Struct book b1={“cpu”,
“xyz”,300,200.50};
Printf(“name of title %s”,b1.title);
Difference between structure and unions
Structure:
1. Each member in the structure is
assigned its own unique storage area.
2. All member can be active at a time.
time.
3. Occupies lot of memory space.
4. Allocates the memory equal to
total memory required by all the
members.
struct ss
{
Int a;
Float b;
Char c;
};
Unions:
1. All member in the union share the
same storage area in the computer’s
memory.
2. Only one member can be active at a
time.
3. Occupies less memory space.
4. Allocate the memory equal
to the maximum memory
required by the member.
Union uu
{
Int a;
Float b;
Char c;
};
Pointers to Structures
You can define pointers to structures in the same way as you define pointer to any other variable:-
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&'; operator before the structure's name as follows:-
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the → operator as follows:-
struct_pointer->title;
Example using structure pointer.
#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 by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
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);
}
Output:
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
Bit Fields
Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium.
- Examples:-
- Packing several objects into a machine word.
- e.g.
- 1 bit flags can be compacted.
- Reading external file formats -- non-standard file formats could be read in, e.g., 9-bit integers.
0 Comments