11 STRUCTURES
Spring 2017 CS 101 Introduction To Computing
B1:
Let Us C 13th Edition, BPB Publications, July 2016
Yashavant Kanetkar
Credits
B1
Assignment
B1:
Read: Ch 11
Problems:
Ch 11: [A] (a), (c); [b] (c), (d); [D] (b), (d), (f), (g)
Why Structures?
Real world entities often have attributes corresponding to different
data type, but, need to be treated together as part of one entity.
A structure contains a number of data types grouped together.
The data types may not be of the same type.
Suppose you want to store data about a book. The book attributes
consists of:
Book Name: a string
Price: a float
Number of pages: an int
WACP that stores the above details of three books and prints the
same.
1. main( ){
2. struct book {
3. char name[20] ;
4. float price ;
5. int pages ;
6. } ;
7. struct book b1, b2, b3 ;
8. printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
9. scanf ( "%s %f %d", &b1.name, &b1.price, &b1.pages ) ;
10. scanf ( "%s %f %d", &b2.name, &b2.price, &b2.pages ) ;
11. scanf ( "%s %f %d", &b3.name, &b3.price, &b3.pages ) ;
12. printf ( "\nAnd this is what you entered" ) ;
13. printf ( "\n%s %f %d", b1.name, b1.price, b1.pages ) ;
14. printf ( "\n%s %f %d", b2.name, b2.price, b2.pages ) ;
15. printf ( "\n%s %f %d", b3.name, b3.price, b3.pages ) ;
16. }
Declaring a Structure
main( ){
struct book {/*Structure Declaration*/
char name[20] ;
float price ;
int pages ;
} ;
struct book b1, b2, b3 ; /*Variables of type struct book*/
The member-elements of a structure are always allotted adjacent memory location.
We can combine the declaration of the structure type and the structure
variables in one statement. The following three declare the same thing:
struct book { struct book { struct {
char name[20] ; char name[20] ; char name[20] ;
float price ; float price ; float price ;
int pages ; int pages ; int pages ;
} ; } b1, b2, b3; } b1, b2, b3;
struct book b1, b2, b3 ;
1. struct book {
2. char name[20] ;
3. float price ;
4. int pages ;
5. } ;
6. main( ){
7. struct book b1, b2, b3 ;
8. printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
9. scanf ( "%s %f %d", &b1.name, &b1.price, &b1.pages ) ;
10. scanf ( "%s %f %d", &b2.name, &b2.price, &b2.pages ) ;
11. scanf ( "%s %f %d", &b3.name, &b3.price, &b3.pages ) ;
12. printf ( "\nAnd this is what you entered" ) ;
13. printf ( "\n%s %f %d", b1.name, b1.price, b1.pages ) ;
14. printf ( "\n%s %f %d", b2.name, b2.price, b2.pages ) ;
15. printf ( "\n%s %f %d", b3.name, b3.price, b3.pages ) ;
16. }
Accessing Structure Elements
printf ( "\n%s %f %d", b1.name, b1.price, b1.pages ) ;
printf ( "\n%s %f %d", b2.name, b2.price, b2.pages ) ;
printf ( "\n%s %f %d", b3.name, b3.price, b3.pages ) ;
Memory Map of Structure Elements
main( ){
struct book Program Output:
{ Address of name = 2293248
char name[50] ; Address of price = 2293300
float price ; Address of pages = 2293304
int pages ;
} ;
struct book b1 = { 'B', 130.00, 550 } ;
printf ( "\nAddress of name = %u", &b1.name ) ;
printf ( "\nAddress of price = %u", &b1.price ) ;
printf ( "\nAddress of pages = %u", &b1.pages ) ;
}
Array of Structures
1. main( ){
2. struct book {
3. char name[50];
4. float price ;
5. int pages ;
6. } ;
7. struct book b[100] ;
8. int i ;
9. for ( i = 0 ; i <= 99 ; i++ ) {
10. printf ( "\nEnter name, price and pages:\n" ) ;
11. scanf ( "%s %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
12. }
13. for ( i = 0 ; i <= 99 ; i++ )
14. printf ( "\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ;
15. }
Copying Structural Elements
1. main( ){ Program Output
2. struct employee {
3. char name[10] ; Sanjay 30 5500.500000
4. int age ; Sanjay 30 5500.500000
5. float salary ; Sanjay 30 5500.500000
6. } ;
7. struct employee e1 = { "Sanjay", 30, 5500.50 } ;/*initialization*/
8. struct employee e2, e3 ;
9. /* piece-wise copying */
10. strcpy ( e2.name, e1.name ) ;
11. e2.age = e1.age ;
12. e2.salary = e1.salary ;
13. /* copying all elements at one go */
14. e3 = e2 ;
15. printf ( "\n%s %d %f", e1.name, e1.age, e1.salary ) ;
16. printf ( "\n%s %d %f", e2.name, e2.age, e2.salary ) ;
17. printf ( "\n%s %d %f", e3.name, e3.age, e3.salary ) ;
18. }
Nesting Structures
main( ){
struct address { Program Output
char phone[15] ; name = jeru phone = 531046
char city[25] ; city = nagpur pin = 10
int pin ;
} ;
struct emp {
char name[25] ;
struct address a ;
} ;
struct emp e = { "jeru", "531046", "nagpur", 10 };
printf ( "\nname = %s phone = %s", e.name, e.a.phone ) ;
printf ( "\ncity = %s pin = %d", e.a.city, e.a.pin ) ;
}
Passing Structure to a Function
struct book {
char name[25] ; Program Output
char author[25] ;
int callno ;
Let us C YPK 101
};
main( ){
struct book b1 = { "Let us C", "YPK", 101 } ;
display ( b1 ) ;
}
display ( struct book b ){
printf ( "\n%s %s %d", b.name, b.author, b.callno ) ;
}
Pointer to a Structure
main( ){
Program Output:
struct book { Let us C YPK 101
char name[25] ; Let us C YPK 101
char author[25] ;
int callno ;
} ;
struct book b1 = { "Let us C", "YPK", 101 } ;
struct book *ptr ;
ptr = &b1 ;
printf ( "\n%s %s %d", b1.name, b1.author, b1.callno ) ;
printf ( "\n%s %s %d", ptr->name, ptr->author, ptr->callno ) ;
}