logging in or signing up Structure12 rajni.b27 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 181 Category: Education License: Some Rights Reserved Like it (0) Dislike it (0) Added: November 11, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Structure : Structure Group of data items of different data types held together in a single unit.a 11/11/2010 Rajni Bhalla CSE/IT Declaring a Structure : Declaring a Structure struct sname { type var1; type var2; type var3; . . type varN; }; struct is a keyword to define a structure. sname is the name given to the structure data type. type is a built-in data type. var1,var2,var3,…..,varN are elements of structure being defined. Structure template sname is called tag makes it possible to declare other variable of the same structure type without having to rewrite the template itself. It’s a type name.tag is optional. 11/11/2010 Rajni Bhalla CSE/IT Eg to store information of an employee, our structure declaration may look like as shown overleaf : Eg to store information of an employee, our structure declaration may look like as shown overleaf struct employee_type { int code; char name[20]; int dept_code; float salary; }; No variable has been associated with this structure No memory is set aside for this structure. 11/11/2010 Rajni Bhalla CSE/IT Declaring Variables of the Structure Type : Declaring Variables of the Structure Type Declares a variable employee of type employee_type At this point, the memory is set aside for the structure variable employee. We can also declare variable(s) of structure type along with the structure declaration. struct employee_type employee; struct employee_type {int code; char name[20]; int dept_code; float salary; }employee 11/11/2010 Rajni Bhalla CSE/IT Consider the declarations to understand how the elements of the structure variables are stored in memory : Consider the declarations to understand how the elements of the structure variables are stored in memory struct example_type { char var1; int var2; float var3; double var4; }; struct example_type sample1; Note: all members are stored in contiguous memory location in order in which they are declared. 11/11/2010 Rajni Bhalla CSE/IT How the elements of the structure variables are stored in memory : How the elements of the structure variables are stored in memory var 1 var 2 var 4 var 3 11/11/2010 Rajni Bhalla CSE/IT Intializing Structures : Intializing Structures struct student_type { int rollno; char name[25]; int age; float height; }; struct student_type student={1000,”Surbhi salaria”,18,5.6}; 11/11/2010 Rajni Bhalla CSE/IT Accessing Structure Elements : Accessing Structure Elements Elements are accessed using dot operator. It provides a powerful and clear way to refer to an individual element. Syntax: sname.vname sname is structure variable name. vname is name of the element of the structure. Eg: the element of the structure variable student can be accessed as student.rollno,student.name,student.age,student.height 11/11/2010 Rajni Bhalla CSE/IT WAP to display size of structure elements.Use sizeof() of operator. : WAP to display size of structure elements.Use sizeof() of operator. main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1; clrscr(); printf(“\nSize of Structure elements\n”); printf(“\nBook: %d”,sizeof(bk1.book)); printf(“\nPages: %d”,sizeof(bk1.pages)); printf(“\nBook:%d”,sizeof(bk1.price)); printf(“\nBook:%d”,sizeof(bk1)); } Output Book : 30 Pages:2 Price:4 Total Bytes:36 11/11/2010 Rajni Bhalla CSE/IT Entering Data into Structures : Entering Data into Structures struct employee_type { int code; char name[25]; char dept[15]; float salary; }; main() { struct employee_type employee; printf(“\nEnter employee code:\n”); scanf(“%d”,&code); printf(“\nEnter name:\n”); gets(employee.name); printf(“\nEnter employee’s dept:\n”); gets(employee.dept); printf(“\nEnter employee’s salary:\n”); scanf(“%f”,&employee.salary); continue printf(“\n\nParticulars of emp as entered by user\n”); printf(“\nEmployees code:%d”,employee.code); printf(“\nEmployee’s name:%s”, employee.name); printf(“\nEmployee’s dept:%s”,employee.dept); Printf(“\nEmployee’s sal:%f”,employee.salary); continue 11/11/2010 Rajni Bhalla CSE/IT Use of Assignment Statement for Structures : Use of Assignment Statement for Structures Value of one structure variable can be assigned to another variable of the same type using simple assignment statement.if student1 and student2 are structure variable of type student_type,then student2=student1; Assigns value of structure variable student1 to student2 Simple assignment cannot be used this way for arrays. 11/11/2010 Rajni Bhalla CSE/IT WAP to copy structure elements from one object to another object : WAP to copy structure elements from one object to another object void main() { struct disk { char co[15]; float type; int price; }; struct disk d1={“SONY”,1.44,20}; struct disk d2,d3; strcpy(d2.co,d1.co); individual elements of d1 object are copied using d2.type=d1.type; assignment statement d2.price=d1.price; d3=d2; // all the contents are copied to d3 clrscr(); continue continue printf(“\n %s %g %d”,d1.co,d1.type,d1.price); printf(“\n %s %g %d”,d2.co,d2.type,d2.price); printf(“\n %s % %d”,d3.co,d3.type,d3.price); } 11/11/2010 Rajni Bhalla CSE/IT Pointers and Structures : Pointers and Structures struct student_type student,*ptr It declares a structures variable student and a pointer variable ptr to structure of type student_type.ptr can be initialized with the following assignment statement ptr=&student; HOW WE CAN ACCESS THE ELEMENTS OF STRUCTURE? But this approach will not work because dot has higher priority Correctly way to write is: or *ptr.rollno,*ptr.name,*ptr.age,*ptr.height (*ptr).rollno,(*ptr).name,(*ptr).age,(*ptr).height ptr->rollno,ptr->name,ptr->age,ptr->height 11/11/2010 Rajni Bhalla CSE/IT Structure within Structure : Structure within Structure We can take object of one structure as member in another structure. Structure within structure can be used to create complex data applications. Syntax: struct time { int second; int time; int hour; } struct t { int carno; struct time st; struct time et; }; struct t player; 11/11/2010 Rajni Bhalla CSE/IT Slide 15: main() { struct time { int second; int minute; int hour; } struct t { int carno; struct time st; struct time rt; }; struct t r1; clrscr(); scanf(“%d”,&r1.carno); scanf(“%d %d %d”,&r1.st.hour,&r1.st.minute,&r1.st.second); scanf(“%d %d %d”,&r1.rt.hour,&r1.rt.minute,&r1.rt.second); printf(“\n \tCArno \n\tStarting time \n\t Reaching time\n”); printf(“\t%d\t”,r1.carno); printf(“\t%d:%d:%d\t\t”,r1.st.hour,ri.st.minute,r1.st.second); printf(“\t%d:%d:%d\t\t”,r1.rt.hour,ri.rt.minute,r1.rt.second); } 11/11/2010 Rajni Bhalla CSE/IT Array of Structures : Array of Structures If we wish to process a list of values of structure type,then we need an array of such structures. 11/11/2010 Rajni Bhalla CSE/IT Declaring an Array of Structures : Declaring an Array of Structures struct employee_type { int code; char name[25]; char dept[15]; float salary; }; struct employee_type employee[50]; 11/11/2010 Rajni Bhalla CSE/IT Accessing elements an array of structures : Accessing elements an array of structures Individual elements of a structure in an array of structure are accessed by referring to structure variable name. Followed by subscript. Followed by dot operator. Ending with structure element desired. Suppose we want to access salary of 7th employee,we can do so by writing employee[6].salary 11/11/2010 Rajni Bhalla CSE/IT WAP to create an array of structure objects : WAP to create an array of structure objects main() { int k; struct time { int second; int minute; int hour; }; struct tt { int carno; struct time st; struct time rt; }; struct tt r1[3]; clrscr(); printf(“\n Car no Starting time Reaching time”); for(k=0;k<3;k++) { scanf(“%d”,&r[k].carno); scanf(“%d %d %d”, &r1[k].st.hour,&r1[k].st.minute,&r1[k].st.second); scanf(“%d %d %d”, &r1[k].rt.hour,&r1[k].rt.minute,&r1[k].rt.second); } for(k=0;k<3;k++) { printf(“%d”,r1[k].carno); printf(“%d %d %d”,r1[k].st.hour,r1[k].st.minute,r1[k].st.second); printf(“%d %d %d”,r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second); } } 11/11/2010 Rajni Bhalla CSE/IT Structure and Function : Structure and Function The relationship of structure with the function can be viewed from three angles:- Passing Structures to a function. Function Returning Structure. Passing array of Structures to Function. Note: When a structure element is to be passed to any other function,it is essential to declare the structure outside the main() functioni.e global 11/11/2010 Rajni Bhalla CSE/IT Passing Structure to a Function : Passing Structure to a Function Similar to passing array of variable,structure can be passed to a function as argument Syntax: type-specifier func-name(struct-variable); /*actual argument*/ 11/11/2010 Rajni Bhalla CSE/IT Read and display student grade by using structure with function : Read and display student grade by using structure with function struct student { int rn; char name[20]; char grade; }s; main() { printf(“\nEnter rollno,name and grade of student:\n”); scanf(“%d %s %c”,&s.rn,s.name,&s.grade); display(s); getche(); } display(m) struct student m; { printf(“\nRollno is %d”,m.rn); printf(“\n Name is %s”,m.name); printf(“\n Grade is: %c”,m.grade); grade; } 11/11/2010 Rajni Bhalla CSE/IT Passing of structure variables by value to a function : Passing of structure variables by value to a function struct date { int day; int month; int year; }; void print_date(struct date); void main() { struct date d={10,12,1997}; print_date(d); } void print_date(struct date a) { printf(“\nDate in format %d %d %d”,a.day,a.month,a.year); } 11/11/2010 Rajni Bhalla CSE/IT Passing to the function by address : Passing to the function by address struct book { char name[35]; char author[35]; int pages; }b1; void main() { --------- --------- show(&b1); } show(struct book *b2) { ------------------ ------------------- } 11/11/2010 Rajni Bhalla CSE/IT Prog to pass address of structure variable to user defined function and display the contents : Prog to pass address of structure variable to user defined function and display the contents struct book { char name[35]; char author[35]; char author[35]; int pages; } void main() { struct book b1={“JAVA COMPETE REFERENCE”,”P.NAUGHTON”,886}; show(&b1); } show(struct book *b2) pointer to structure { clrscr(); printf(“%s %s %d”,b2->name,b2->author,b2->pages); } 11/11/2010 Rajni Bhalla CSE/IT Passing of structure variables by reference to a function : Passing of structure variables by reference to a function struct date { int day; int month; int year; }; main() { struct date d; void get_date(struct date *); printf(“Enter date in the format:\n”); get_date(&d); printf(“\nDate entered by you %d %d %d”,d.day,d.month,d.year); } void get_date(struct date *a) { scanf(“%d %d %d”,&a->day,&a->month,a->year); } 11/11/2010 Rajni Bhalla CSE/IT Wap to pass structure elements to function print() and print the elements : Wap to pass structure elements to function print() and print the elements void main() { struct boy { char name[25]; int age; int wt; }; struct boy b1={“Amit”,20,25}; print(b1.name,b1.age,b1.wt); } print(char *s,int t, int n) { printf(“\n%s %d %d”,s,t,n); return 0; } Note: We have passed base address of name But values of age and wt. Here values are passed using call by reference and call by value 11/11/2010 Rajni Bhalla CSE/IT Function Returning Structure : Function Returning Structure struct date { int day; int month; int year; }; main() { struct date d; struct date get_date(void); printf(“\nEnter date in format day/month/year”); d=get_date(); printf(“\nDAte entered by you is %d %d %d\n”,d.day,d.month,d.year); } struct date get_date(void) { struct date a; scanf(“%d %d %d”,&a.day,&a.month,&a.year); return a; } 11/11/2010 Rajni Bhalla CSE/IT Self Referential Structure(SRS) : Self Referential Structure(SRS) The SRS are structures that include an element which is a pointer to another structure of the same type. When a member of structure declared as a pointer to the structure itself then the structure is called SRS. Structures find their applications in building complex data structure such as linked lists, trees and graphs. 11/11/2010 Rajni Bhalla CSE/IT Consider following declaration:- : Consider following declaration:- struct link { int data; // data is a variable of type int struct link *p; // p is a pointer to structure of type link }; struct link A,B; //declares structure variable A and B A.p=&B; // connect A to B we observe that pointer p of structure variable B is dangling i.e it is pointing nowhere Such pointer can be assigned to null. 11/11/2010 Rajni Bhalla CSE/IT Bit Fields : Bit Fields In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a "bit field." Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type. 11/11/2010 Rajni Bhalla CSE/IT . : . Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal: short a:17; /* Illegal! */ int long y:33; /* Illegal! */ This example defines a two-dimensional array of structures named screen. Struct { unsigned short icon : 8; unsigned short color : 4; unsigned short underline : 1; unsigned short blink : 1; } screen[25][80]; The array contains 2,000 elements. Each element is an individual structure containing four bit-field members: icon, color, underline, and blink. The size of each structure is two bytes. Bit fields have the same semantics as the integer type. This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field. 11/11/2010 Rajni Bhalla CSE/IT Slide 33: Union Union: Union is similar as structure. The major distinction between them in terms of storage. In structure each member has its own storage location whereas all the members of union uses the same location. The union may contain many members of different data type it can handle only one member at a time union can be declared using the keyword union. Hold only one object at a time Union requires bytes that are equal to the number of bytes required for the largest members. Eg: union contains char,int &long then the number of bytes reserved in the member for the union is 4 bytes. 11/11/2010 Rajni Bhalla CSE/IT Slide 34: union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is=sizeof(float); Size of in bytes of the union is size of the largest variable element in the union. Slide 35: Union item { int m; float x; char c; } code; This declare a variable code of type union item. 11/11/2010 Rajni Bhalla CSE/IT output : output main() { union result { int marks; char grade; }; struct res { char name[15]; int age; union result perf; }data; printf(“Size of union:%d”,sizeof(data.perf)); printf(“Size of structure:%d”,sizeof(data)); } Output: Size of union: 2 Size of structure: 19 11/11/2010 Rajni Bhalla CSE/IT Slide 37: main() { int m,n,p,q,a[5][5],b[5][5],c[5][5],I,j,k; printf(“\nEnter the valur row and columns:\n”); scanf(“%d %d”,&m,&n); scanf(“%d %d”,&p,&q); if(n==p) { printf(“\nEnter 1st matrix:\n”); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j+) scanf(“%d”,&a[i][j]); } for(i=0;i<=p-1;i++) { for(j=0;j<=q-1;j+) scanf(“%d”,&a[i][j]); } for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) {c[i][j]=0; for(k=0;k<=n-1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } 11/11/2010 Rajni Bhalla CSE/IT You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Structure12 rajni.b27 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 181 Category: Education License: Some Rights Reserved Like it (0) Dislike it (0) Added: November 11, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Structure : Structure Group of data items of different data types held together in a single unit.a 11/11/2010 Rajni Bhalla CSE/IT Declaring a Structure : Declaring a Structure struct sname { type var1; type var2; type var3; . . type varN; }; struct is a keyword to define a structure. sname is the name given to the structure data type. type is a built-in data type. var1,var2,var3,…..,varN are elements of structure being defined. Structure template sname is called tag makes it possible to declare other variable of the same structure type without having to rewrite the template itself. It’s a type name.tag is optional. 11/11/2010 Rajni Bhalla CSE/IT Eg to store information of an employee, our structure declaration may look like as shown overleaf : Eg to store information of an employee, our structure declaration may look like as shown overleaf struct employee_type { int code; char name[20]; int dept_code; float salary; }; No variable has been associated with this structure No memory is set aside for this structure. 11/11/2010 Rajni Bhalla CSE/IT Declaring Variables of the Structure Type : Declaring Variables of the Structure Type Declares a variable employee of type employee_type At this point, the memory is set aside for the structure variable employee. We can also declare variable(s) of structure type along with the structure declaration. struct employee_type employee; struct employee_type {int code; char name[20]; int dept_code; float salary; }employee 11/11/2010 Rajni Bhalla CSE/IT Consider the declarations to understand how the elements of the structure variables are stored in memory : Consider the declarations to understand how the elements of the structure variables are stored in memory struct example_type { char var1; int var2; float var3; double var4; }; struct example_type sample1; Note: all members are stored in contiguous memory location in order in which they are declared. 11/11/2010 Rajni Bhalla CSE/IT How the elements of the structure variables are stored in memory : How the elements of the structure variables are stored in memory var 1 var 2 var 4 var 3 11/11/2010 Rajni Bhalla CSE/IT Intializing Structures : Intializing Structures struct student_type { int rollno; char name[25]; int age; float height; }; struct student_type student={1000,”Surbhi salaria”,18,5.6}; 11/11/2010 Rajni Bhalla CSE/IT Accessing Structure Elements : Accessing Structure Elements Elements are accessed using dot operator. It provides a powerful and clear way to refer to an individual element. Syntax: sname.vname sname is structure variable name. vname is name of the element of the structure. Eg: the element of the structure variable student can be accessed as student.rollno,student.name,student.age,student.height 11/11/2010 Rajni Bhalla CSE/IT WAP to display size of structure elements.Use sizeof() of operator. : WAP to display size of structure elements.Use sizeof() of operator. main() { struct book1 { char book[30]; int pages; float price; }; struct book1 bk1; clrscr(); printf(“\nSize of Structure elements\n”); printf(“\nBook: %d”,sizeof(bk1.book)); printf(“\nPages: %d”,sizeof(bk1.pages)); printf(“\nBook:%d”,sizeof(bk1.price)); printf(“\nBook:%d”,sizeof(bk1)); } Output Book : 30 Pages:2 Price:4 Total Bytes:36 11/11/2010 Rajni Bhalla CSE/IT Entering Data into Structures : Entering Data into Structures struct employee_type { int code; char name[25]; char dept[15]; float salary; }; main() { struct employee_type employee; printf(“\nEnter employee code:\n”); scanf(“%d”,&code); printf(“\nEnter name:\n”); gets(employee.name); printf(“\nEnter employee’s dept:\n”); gets(employee.dept); printf(“\nEnter employee’s salary:\n”); scanf(“%f”,&employee.salary); continue printf(“\n\nParticulars of emp as entered by user\n”); printf(“\nEmployees code:%d”,employee.code); printf(“\nEmployee’s name:%s”, employee.name); printf(“\nEmployee’s dept:%s”,employee.dept); Printf(“\nEmployee’s sal:%f”,employee.salary); continue 11/11/2010 Rajni Bhalla CSE/IT Use of Assignment Statement for Structures : Use of Assignment Statement for Structures Value of one structure variable can be assigned to another variable of the same type using simple assignment statement.if student1 and student2 are structure variable of type student_type,then student2=student1; Assigns value of structure variable student1 to student2 Simple assignment cannot be used this way for arrays. 11/11/2010 Rajni Bhalla CSE/IT WAP to copy structure elements from one object to another object : WAP to copy structure elements from one object to another object void main() { struct disk { char co[15]; float type; int price; }; struct disk d1={“SONY”,1.44,20}; struct disk d2,d3; strcpy(d2.co,d1.co); individual elements of d1 object are copied using d2.type=d1.type; assignment statement d2.price=d1.price; d3=d2; // all the contents are copied to d3 clrscr(); continue continue printf(“\n %s %g %d”,d1.co,d1.type,d1.price); printf(“\n %s %g %d”,d2.co,d2.type,d2.price); printf(“\n %s % %d”,d3.co,d3.type,d3.price); } 11/11/2010 Rajni Bhalla CSE/IT Pointers and Structures : Pointers and Structures struct student_type student,*ptr It declares a structures variable student and a pointer variable ptr to structure of type student_type.ptr can be initialized with the following assignment statement ptr=&student; HOW WE CAN ACCESS THE ELEMENTS OF STRUCTURE? But this approach will not work because dot has higher priority Correctly way to write is: or *ptr.rollno,*ptr.name,*ptr.age,*ptr.height (*ptr).rollno,(*ptr).name,(*ptr).age,(*ptr).height ptr->rollno,ptr->name,ptr->age,ptr->height 11/11/2010 Rajni Bhalla CSE/IT Structure within Structure : Structure within Structure We can take object of one structure as member in another structure. Structure within structure can be used to create complex data applications. Syntax: struct time { int second; int time; int hour; } struct t { int carno; struct time st; struct time et; }; struct t player; 11/11/2010 Rajni Bhalla CSE/IT Slide 15: main() { struct time { int second; int minute; int hour; } struct t { int carno; struct time st; struct time rt; }; struct t r1; clrscr(); scanf(“%d”,&r1.carno); scanf(“%d %d %d”,&r1.st.hour,&r1.st.minute,&r1.st.second); scanf(“%d %d %d”,&r1.rt.hour,&r1.rt.minute,&r1.rt.second); printf(“\n \tCArno \n\tStarting time \n\t Reaching time\n”); printf(“\t%d\t”,r1.carno); printf(“\t%d:%d:%d\t\t”,r1.st.hour,ri.st.minute,r1.st.second); printf(“\t%d:%d:%d\t\t”,r1.rt.hour,ri.rt.minute,r1.rt.second); } 11/11/2010 Rajni Bhalla CSE/IT Array of Structures : Array of Structures If we wish to process a list of values of structure type,then we need an array of such structures. 11/11/2010 Rajni Bhalla CSE/IT Declaring an Array of Structures : Declaring an Array of Structures struct employee_type { int code; char name[25]; char dept[15]; float salary; }; struct employee_type employee[50]; 11/11/2010 Rajni Bhalla CSE/IT Accessing elements an array of structures : Accessing elements an array of structures Individual elements of a structure in an array of structure are accessed by referring to structure variable name. Followed by subscript. Followed by dot operator. Ending with structure element desired. Suppose we want to access salary of 7th employee,we can do so by writing employee[6].salary 11/11/2010 Rajni Bhalla CSE/IT WAP to create an array of structure objects : WAP to create an array of structure objects main() { int k; struct time { int second; int minute; int hour; }; struct tt { int carno; struct time st; struct time rt; }; struct tt r1[3]; clrscr(); printf(“\n Car no Starting time Reaching time”); for(k=0;k<3;k++) { scanf(“%d”,&r[k].carno); scanf(“%d %d %d”, &r1[k].st.hour,&r1[k].st.minute,&r1[k].st.second); scanf(“%d %d %d”, &r1[k].rt.hour,&r1[k].rt.minute,&r1[k].rt.second); } for(k=0;k<3;k++) { printf(“%d”,r1[k].carno); printf(“%d %d %d”,r1[k].st.hour,r1[k].st.minute,r1[k].st.second); printf(“%d %d %d”,r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second); } } 11/11/2010 Rajni Bhalla CSE/IT Structure and Function : Structure and Function The relationship of structure with the function can be viewed from three angles:- Passing Structures to a function. Function Returning Structure. Passing array of Structures to Function. Note: When a structure element is to be passed to any other function,it is essential to declare the structure outside the main() functioni.e global 11/11/2010 Rajni Bhalla CSE/IT Passing Structure to a Function : Passing Structure to a Function Similar to passing array of variable,structure can be passed to a function as argument Syntax: type-specifier func-name(struct-variable); /*actual argument*/ 11/11/2010 Rajni Bhalla CSE/IT Read and display student grade by using structure with function : Read and display student grade by using structure with function struct student { int rn; char name[20]; char grade; }s; main() { printf(“\nEnter rollno,name and grade of student:\n”); scanf(“%d %s %c”,&s.rn,s.name,&s.grade); display(s); getche(); } display(m) struct student m; { printf(“\nRollno is %d”,m.rn); printf(“\n Name is %s”,m.name); printf(“\n Grade is: %c”,m.grade); grade; } 11/11/2010 Rajni Bhalla CSE/IT Passing of structure variables by value to a function : Passing of structure variables by value to a function struct date { int day; int month; int year; }; void print_date(struct date); void main() { struct date d={10,12,1997}; print_date(d); } void print_date(struct date a) { printf(“\nDate in format %d %d %d”,a.day,a.month,a.year); } 11/11/2010 Rajni Bhalla CSE/IT Passing to the function by address : Passing to the function by address struct book { char name[35]; char author[35]; int pages; }b1; void main() { --------- --------- show(&b1); } show(struct book *b2) { ------------------ ------------------- } 11/11/2010 Rajni Bhalla CSE/IT Prog to pass address of structure variable to user defined function and display the contents : Prog to pass address of structure variable to user defined function and display the contents struct book { char name[35]; char author[35]; char author[35]; int pages; } void main() { struct book b1={“JAVA COMPETE REFERENCE”,”P.NAUGHTON”,886}; show(&b1); } show(struct book *b2) pointer to structure { clrscr(); printf(“%s %s %d”,b2->name,b2->author,b2->pages); } 11/11/2010 Rajni Bhalla CSE/IT Passing of structure variables by reference to a function : Passing of structure variables by reference to a function struct date { int day; int month; int year; }; main() { struct date d; void get_date(struct date *); printf(“Enter date in the format:\n”); get_date(&d); printf(“\nDate entered by you %d %d %d”,d.day,d.month,d.year); } void get_date(struct date *a) { scanf(“%d %d %d”,&a->day,&a->month,a->year); } 11/11/2010 Rajni Bhalla CSE/IT Wap to pass structure elements to function print() and print the elements : Wap to pass structure elements to function print() and print the elements void main() { struct boy { char name[25]; int age; int wt; }; struct boy b1={“Amit”,20,25}; print(b1.name,b1.age,b1.wt); } print(char *s,int t, int n) { printf(“\n%s %d %d”,s,t,n); return 0; } Note: We have passed base address of name But values of age and wt. Here values are passed using call by reference and call by value 11/11/2010 Rajni Bhalla CSE/IT Function Returning Structure : Function Returning Structure struct date { int day; int month; int year; }; main() { struct date d; struct date get_date(void); printf(“\nEnter date in format day/month/year”); d=get_date(); printf(“\nDAte entered by you is %d %d %d\n”,d.day,d.month,d.year); } struct date get_date(void) { struct date a; scanf(“%d %d %d”,&a.day,&a.month,&a.year); return a; } 11/11/2010 Rajni Bhalla CSE/IT Self Referential Structure(SRS) : Self Referential Structure(SRS) The SRS are structures that include an element which is a pointer to another structure of the same type. When a member of structure declared as a pointer to the structure itself then the structure is called SRS. Structures find their applications in building complex data structure such as linked lists, trees and graphs. 11/11/2010 Rajni Bhalla CSE/IT Consider following declaration:- : Consider following declaration:- struct link { int data; // data is a variable of type int struct link *p; // p is a pointer to structure of type link }; struct link A,B; //declares structure variable A and B A.p=&B; // connect A to B we observe that pointer p of structure variable B is dangling i.e it is pointing nowhere Such pointer can be assigned to null. 11/11/2010 Rajni Bhalla CSE/IT Bit Fields : Bit Fields In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a "bit field." Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type. 11/11/2010 Rajni Bhalla CSE/IT . : . Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal: short a:17; /* Illegal! */ int long y:33; /* Illegal! */ This example defines a two-dimensional array of structures named screen. Struct { unsigned short icon : 8; unsigned short color : 4; unsigned short underline : 1; unsigned short blink : 1; } screen[25][80]; The array contains 2,000 elements. Each element is an individual structure containing four bit-field members: icon, color, underline, and blink. The size of each structure is two bytes. Bit fields have the same semantics as the integer type. This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field. 11/11/2010 Rajni Bhalla CSE/IT Slide 33: Union Union: Union is similar as structure. The major distinction between them in terms of storage. In structure each member has its own storage location whereas all the members of union uses the same location. The union may contain many members of different data type it can handle only one member at a time union can be declared using the keyword union. Hold only one object at a time Union requires bytes that are equal to the number of bytes required for the largest members. Eg: union contains char,int &long then the number of bytes reserved in the member for the union is 4 bytes. 11/11/2010 Rajni Bhalla CSE/IT Slide 34: union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is=sizeof(float); Size of in bytes of the union is size of the largest variable element in the union. Slide 35: Union item { int m; float x; char c; } code; This declare a variable code of type union item. 11/11/2010 Rajni Bhalla CSE/IT output : output main() { union result { int marks; char grade; }; struct res { char name[15]; int age; union result perf; }data; printf(“Size of union:%d”,sizeof(data.perf)); printf(“Size of structure:%d”,sizeof(data)); } Output: Size of union: 2 Size of structure: 19 11/11/2010 Rajni Bhalla CSE/IT Slide 37: main() { int m,n,p,q,a[5][5],b[5][5],c[5][5],I,j,k; printf(“\nEnter the valur row and columns:\n”); scanf(“%d %d”,&m,&n); scanf(“%d %d”,&p,&q); if(n==p) { printf(“\nEnter 1st matrix:\n”); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j+) scanf(“%d”,&a[i][j]); } for(i=0;i<=p-1;i++) { for(j=0;j<=q-1;j+) scanf(“%d”,&a[i][j]); } for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) {c[i][j]=0; for(k=0;k<=n-1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } 11/11/2010 Rajni Bhalla CSE/IT