STRUCTURES Library system Books name a string Books
STRUCTURES
Library system : Book’s name (a string), Book’s price (a float) number of pages in it (an int). SOLUTION Construct individual arrays, one for storing names, one for storing prices, one for storing number of pages. Use a structure variable.
OUTPUT Enter names, prices and no. of pages A 100. 00 354 C 256. 50 682 F 233. 70 512 main( ) { And this is what you entered A 100. 000000 354 char name[3] ; C 256. 500000 682 float price[3] ; F 233. 700000 512 int pages[3], i ; printf ( "n. Enter names, prices and no. of pagesn" ) ; for ( i = 0 ; i <= 2 ; i++ ) scanf ( "%c %f %d", &name[i], &price[i], &pages[i] ); printf ( "n. And this is what you enteredn" ) ; for ( i = 0 ; i <= 2 ; i++ ) printf ( "%c %f %dn", name[i], price[i], pages[i] ); }
STRUCTURE A structure contains a number of data types grouped together. These data types may or may not be of the same type.
DECLARATION A DATA STRUCTURE struct <structure name> { structure element 1 ; structure element 2 ; structure element 3 ; . . . struct book { char name[22] ; float price ; int pages ; }; struct book b 1 , b 2, b 3 ; }; variables b 1 can be declared to be of the type struct book
Declarations & Definitions struct employee { char name[20] ; int age ; float salary ; }; struct employee e = { ”Rahul”, 25, 4000. 50 } ; OR struct employee { char name[20] ; int age ; float salary ; } e = { ”Rahul”, 25, 4000. 50 } , x, y;
MEMORY ALLOCATION struct book b 1, b 2, b 3 ; it makes available space to hold all the elements in the structure— in this case, 7 bytes—one for name, four for price and two for pages. These bytes are always in adjacent memory locations.
Copying piecemeal copying main( ) { struct emp { char n[20] ; int a ; float s ; }; struct emp e 1 = { ”Rahul”, 23, 4000. 50 } ; struct emp e 2, e 3 ; strcpy ( e 2. n, e 1. n ) ; e 2. n = e 1. n ; e 2. a = e 1. a ; e 2. s = e 1. s ; copying at } e 3 = e 1 ; printf ( ”%s %d %f”, one shot e 3. n, e 3. a, e 3. s ) ;
Copying Arrays int a[10] = { 3, 6, 5, … } ; int b[10] ; for ( i = 0 ; i <= 9 ; i++ ) b[i] = a[i] ; OR struct z { int arr[10] ; }; struct z a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ; struct z b ; b=a;
WAP TO DISPLAY SIZE OF STRUCTURE ELEMENTSU. SE SIZEOF() OF OPERATOR. main() { struct book 1 { char book[30]; int pages; float price; }; struct book 1 bk 1; Output Book : 30 Pages: 2 Price: 4 Total Bytes: 36 printf(“n. Size of Structure elementsn”); printf(“n. Book: %d”, sizeof(bk 1. book)); printf(“n. Pages: %d”, sizeof(bk 1. pages)); printf(“n. Price: %d”, sizeof(bk 1. price)); printf(“n. Total Bytes: %d”, sizeof(bk 1)); }
struct result { int rno, mrks[3]; char nm; } res; void main() { int i, total=0; float per; printf("nt Enter Roll Number : "); scanf("%d", &res. rno); printf("nt Enter Marks of 3 Subjects : "); for(i=0; i<3; i++) { scanf("%d", &res. mrks[i]); total = total + res. mrks[i]; } per= total/3; printf("nnt Roll Number : %d", res. rno); printf("nnt Marks are : "); for(i=0; i<3; i++) { printf(" %d", res. mrks[i]); } printf("nnt Percentage is : %f %“, per); } Enter Roll Number : 1 Enter Marks of 3 Subjects : 80 86 80 Roll Number : 1 Marks are : 80 86 80 Percentage is : 82. 00 %
STRUCTURE WITH ARRAYS Enter Employee ID : 1 Employee Name : ABC Enter Employee ID : 2 Employee Name : XYZ Employee ID : 1 Employee Name : ABC Employee ID : 2 Employee Name : XYZ_ struct emp_info { int emp_id; char nm[50]; } emp[2]; void main() { int i; for(i=0; i<2; i++) { printf("nnt Enter Employee ID : "); scanf("%d", &emp[i]. emp_id); printf("nnt Employee Name : "); scanf("%s", emp[i]. nm); } for(i=0; i<2; i++) { printf("nt Employee ID : %d", emp[i]. emp_id); printf("nt Employee Name : %s", emp[i]. nm); } }
Nested Structures main( ) { struct address f - int { char city[20] ; long int pin; printf ( ”%d”, a. b. c. d. e. f ) ; }; struct emp { char n[20] ; int age ; struct address a ; float s ; }; struct emp e = { ”Rahul”, 23, ”Nagpur”, 440010, 4000. 50 } ; printf ( ”%s %d %s %ld %f”, e. n, e. age, e. city, e. pin, e. s ) ; } e. a. city e. a. pin
struct stud_Res { int rno; char std[10]; struct stud_Marks { char subj_cd[30]; int subj_mark; } marks; } result; Enter Roll Number : 1 Enter Standard : F. Y. Enter Subject Code : SUB 001 Enter Marks : 63 Roll Number : 1 Standard : F. Y. . Subject Code : SUB 001 Marks : 63_ void main() { printf("nt Enter Roll Number : "); scanf("%d", &result. rno); printf("nt Enter Standard : "); scanf("%s", result. std); printf("nt Enter Subject Code : "); scanf("%s", result. marks. subj_cd); printf("nt Enter Marks : "); scanf("%d", &result. marks. subj_mark); printf("nnt Roll Number : %d", result. rno); printf("nnt Standard : %s", result. std); printf("n. Subject Code : %s", result. marks. subj_nm); printf("nnt Marks : %d", result. marks. subj_mark); }
Passing Structure Elements main( ) { struct book { char n[20] ; int nop ; float pr ; }; struct book b = { ”Basic”, 425, 135. 00 } ; display ( b. n, b. nop, b. pr ) ; show ( b. n, &b. nop, &b. pr ) ; } display ( char *n, int pg, float p ) { printf ( ”%s %d %f”, n, pg, p ) ; } show ( char *n, int *pg, float *p ) { printf ( ”%s %d %f”, n, *pg, *p ) ; }
Passing Structures main( ) { struct book { char n[20] ; int nop ; float pr ; }; struct book b = { ”Basic”, 425, 135. 00 } ; display 1 ( b ) ; show 1 ( &b ) ; } display 1 ( struct book bb ) { printf ( ”%s %d %f”, bb. nop, bb. pr ) ; } show 1 ( struct book *bb ) { printf ( ”%s %d %f”, ( *bb ). nop, ( *bb ). pr ) ; printf ( ”%s %d %f”, bb -> nop, bb -> pr ) ; }
Complex Nos. main( ) { struct com { float r, i ; }; struct com a = { 2. 5, 1. 3 } ; struct com b = { 1. 2, 1. 7 } ; struct com c ; struct com add ( struct com, struct com ) ; c = add ( a, b ) ; printf ( ”%f %f”, c. r, c. i ) ; } struct com add struct com x, ystruct com y ) add ( struct com( x, struct com ) { struct com z ; x. r = + x. r y. r + ; y. r ; z. r x. i z. i += y. i x. i ; + y. i ; return z ; }
ASSIGNMENT 4 1 - What is nesting of structures and union ? 2 - What is sorting ? Write a program for insertion sort using pointers.
ADVANTAGES OF STRUCTURE Structure has several advantages: � It clarifies the code by showing that the data defined in the structure are intimately related. � It simplifies passing the data to functions. Instead of passing multiple variables separately, they can be passed as a single unit. � It increases the locality of the code. 20
POINTERS AND STRUCTURES The way we can have pointer pointing to an int , or a pointer pointing to a char, similarly we can also have a pointer pointing to a struct. Example : struct date { int month, day, year; }; struct date today, *date_ptr;
STRUCTURE POINTER OPERATOR “->” Pointers to structures are so often used in C that a special operator exists. The structure pointer operator “->” It permits expressions that would otherwise be written as, (*x). y to be more clearly expressed as x>y
EXAMPLE : STRUCTURE POINTERS main() { struct date { int month, day, year; }; struct date today, *date_ptr; date_ptr = &today; date_ptr->day = 11; date_ptr->month = 11; date_ptr->year = 2011; printf("Todays date is %d/%d/%d. n", date_ptr->day, date_ptr->month, date_ptr->year ); }
NOTE : STRUCTURE POINTERS Notice that the syntax date_ptr->day is equivalent to (*date_ptr). day. The parentheses in this access are required, because along with () and [], the operators ‘. ’ and ‘->’ have the highest precedence and associate from left to right. Therefore *date_ptr. day would be a syntax error because date_ptr. day can not be evaluated.
SELF-REFERENTIAL STRUCTURES Self-referential structures �Structure that contains a pointer to a structure of the same type �Can be linked together to form useful data structures such as lists, queues, stacks and trees �Terminated with a NULL pointer (0)
USE OF SELF-REFERENTIAL STRUCTURES Following is an example of this kind of structure: struct_name { datatypename; struct_name * pointer_name; };
EXAMPLE : SELF-REFERENTIAL STRUCTURES struct listnode { int data; struct listnode *next; }; In the above example, the listnode is a selfreferential structure – because the *next is of the type sturct listnode.
EXAMPLE : SELF-REFERENTIAL STRUCTURES 1 *listnode 2 *listnode 3 NULL listnode
EXAMPLE : SELF-REFERENTIAL STRUCTURES Diagram of two self-referential structure objects linked together 15 Data member and pointer 10 NULL pointer (points to nothing)
PASSING STRUCTURE TO A FUNCTION… Structure variables may be passed as arguments and returned from functions just like other scalar variables i. e. int , char.
EXAMPLE : PASSING STRUCTURE TO A FUNCTION… struct_type { int a, b; } ; void f 1(struct_type parm) { printf("%d", parm. a); } void main(void) { struct_type arg; arg. a = 1000; f 1(arg); }
PROBLEM IN PASSING STRUCTURE… Here, a structure is passed to the function. Recall that in C, all parameters are passed by value - the value of each argument expression is copied from the calling function into the cell allocated for the parameter of the called function. Again, for large structures, this may not be a very efficient way to pass data to functions.
REMEDY : PASSING STRUCTURE… passing and returning structures to functions may not be efficient, particularly if the structure is large. We can eliminate this excessive data movement by passing pointers to the structures to the function, and access them indirectly through the pointers. 39
EXAMPLE : PASSING STRUCTURE TO A FUNCTION… struct_type { int a, b; } ; void f 1(struct_type *parm) { printf("%d", parm->a); } void main(void) { struct_type arg; arg. a = 1000; f 1(&arg); } 40
EXAMPLE : RETURNING STRUCTURE FROM A FUNCTION… struct_type { int a, b; } ; struct_type f 1(int x) { struct_type param; param. a = x; return param; } void main(void) { struct_type arg; arg = f 1(1000); } 41
EXAMPLE : RETURNING STRUCTURE FROM A FUNCTION… struct_type { int a, b; } ; struct_type *f 1(int x) { struct_type *param; param->a = x; return param; } void main(void) { struct_type *arg; arg = f 1(1000); } 42
- Slides: 42