420 likes | 606 Views
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.
E N D
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 And this is what you entered A 100.000000 354 C 256.500000 682 F 233.700000 512 main( ) { char name[3] ; float price[3] ; int pages[3], i ; printf ( "\nEnter names, prices and no. of pages\n" ) ; for ( i = 0 ; i <= 2 ; i++ ) scanf ( "%c %f %d", &name[i], &price[i], &pages[i] ); printf ( "\nAnd this is what you entered\n" ) ; for ( i = 0 ; i <= 2 ; i++ ) printf ( "%c %f %d\n", 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 book { char name[22] ; float price ; int pages ; } ; struct book b1 ; variables b1 can be declared to be of the type struct book struct <structure name> { structure element 1 ; structure element 2 ; structure element 3 ; ...... ...... } ; , b2, b3
Declarations & Definitions struct employee { charname ; intage ; floatsalary ; } ; struct employee e [20] = { ”Rahul”, 25, 4000.50 } ; OR struct employee { charname[20] ; intage ; floatsalary ; } e = {”Rahul”, 25, 4000.50 } ; , x, y
Memory Allocation • struct book b1, b2, b3 ; • 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 main( ) { struct emp { char n[20] ; int a ; float s ; } ; struct emp e1 = { ”Rahul”, 23, 4000.50 } ; piecemeal copying struct emp e2, e3 ; e2.n, e1.n strcpy ( ) ; e2.n = e1.n ; e2.a = e1.a ; e2.s = e1.s ; copying at one shot e3 = e1 ; printf( ”%s %d %f”, e3.n, e3.a, e3.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 elements. Use sizeof() of operator. Output Book : 30 Pages:2 Price:4 Total Bytes:36 main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1; printf(“\nSize of Structure elements\n”); printf(“\nBook: %d”,sizeof(bk1.book)); printf(“\nPages: %d”,sizeof(bk1.pages)); printf(“\nPrice:%d”,sizeof(bk1.price)); printf(“\nTotal Bytes:%d”,sizeof(bk1)); }
struct result { intrno, mrks[3]; char nm; } res; void main() { inti, total=0; float per; printf("\n\t Enter Roll Number : "); scanf("%d",&res.rno); printf("\n\t 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("\n\n\t Roll Number : %d",res.rno); printf("\n\n\t Marks are :"); for(i=0;i<3;i++) { printf(" %d",res.mrks[i]); } printf("\n\n\t 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_ structemp_info { intemp_id; char nm[50]; } emp[2]; void main() { inti; for(i=0;i<2;i++) { printf("\n\n\t Enter Employee ID : "); scanf("%d",&emp[i].emp_id); printf("\n\n\t Employee Name : "); scanf("%s",emp[i].nm); } for(i=0;i<2;i++) { printf("\n\t Employee ID : %d",emp[i].emp_id); printf("\n\t Employee Name : %s",emp[i].nm); } }
Nested Structures main( ) { struct address { char city[20] ; long int pin; } ; f - int printf ( ”%d”, a.b.c.d.e.f ) ; struct emp { char n[20] ; int age ; struct address a ; float s ; } ; structemp e = { ”Rahul”, 23, ”Nagpur”, 440010, 4000.50 } ; e.age, %s %ld printf ( ”%s %d %f”, e.n, e.city, e.pin, e.s ) ; } e.a.city a.city e.a.pin
Enter Roll Number : 1 Enter Standard : F.Y. Enter Subject Code : SUB001 Enter Marks : 63 Roll Number : 1 Standard : F.Y.. Subject Code : SUB001 Marks : 63_ structstud_Res { intrno; char std[10]; structstud_Marks { char subj_cd[30]; intsubj_mark; } marks; } result; void main() { printf("\n\t Enter Roll Number : "); scanf("%d",&result.rno); printf("\n\t Enter Standard : "); scanf("%s",result.std); printf("\n\t Enter Subject Code : "); scanf("%s",result.marks.subj_cd); printf("\n\t Enter Marks : "); scanf("%d",&result.marks.subj_mark); printf("\n\n\t Roll Number : %d",result.rno); printf("\n\n\t Standard : %s",result.std); printf("\nSubject Code : %s",result.marks.subj_nm); printf("\n\n\t 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 ( float p ) char *n, int pg, { printf ( ”%s %d %f”, n, pg, p ) ; } char *n, int *pg, show ( float *p ) { ”%s %d %f”, *pg, n, printf( *p ) ; }
Passing Structures main( ) { struct book { char n[20] ; int nop ; float pr ; } ; struct book b = { ”Basic”, 425, 135.00 } ; display1 ( b ) ; show1 ( &b ) ; } display1 ( ) struct book bb { bb.pr ) ; printf( ”%s %d %f”, bb.n, bb.nop, } show1 ( ) struct book *bb { ( * ) bb .nop, ( * ) ”%s %d %f”, bb .n, printf( ( * ) bb .pr ) ; ”%s %d %f”, bb -> n , bb -> nop, printf( 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, struct com y ) add ( struct com x, struct com y ) { struct com z ; x.r + y.r ; z.r = x.r + y.r ; x.i + y.i ; z.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.
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 struct_name{ datatype datatypename; struct_name * pointer_name;};
Example : Self-referential structures • struct listnode { int data; struct listnode *next;}; • In the above example, the listnode is a self-referential structure – because the *next is of the type sturct listnode.
Example : Self-referential structures 1 2 3 NULL *listnode *listnode listnode listnode listnode
Data member and pointer NULL pointer (points to nothing) 10 15 Example : Self-referential structures • Diagram of two self-referential structure objects linked together
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 struct_type { int a, b;} ;voidf1(struct struct_type parm){ printf("%d", parm.a);}void main(void){ struct struct_type arg; arg.a = 1000; f1(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.
Example : Passing structure to a function… • struct struct_type { int a, b;} ;voidf1(struct struct_type *parm){ printf("%d", parm->a);}void main(void){ struct struct_type arg; arg.a = 1000; f1(&arg); }
Example : Returning structure from a function… struct struct_type { int a, b;} ;struct_typef1(int x){ struct struct_type param;param.a = x; return param; }void main(void){ struct struct_type arg; arg = f1(1000); }
Example : Returning structure from a function… struct struct_type { int a, b;} ;struct_type *f1(int x){ struct struct_type *param;param->a = x; return param; }void main(void){ struct struct_type *arg; arg = f1(1000); }