Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure As Function Arguments

Similar presentations


Presentation on theme: "Structure As Function Arguments"— Presentation transcript:

1 Structure As Function Arguments

2 struct point { int x; int y; }; point sum (point p1, point p2) point t; t.x=p1.x+p2.x; t.y=p1.y+p2.y; return t; } void main () point p1={2,2}; point p2={3,3}; point p3=sum(p1,p2); cout<<p3.x<<endl; cout<<p3.y<<endl;

3 Nested Structures

4 Nested Structures One Structure can be nested into another structure.
Example struct Distance { int feet; float inches; }; struct Room char name[20]; Distance length; Distance width;

5 Accessing Nested Structure Members
To access the nested structure members, we have to apply dot operator twice. Example Room Dining; strcpy(Dining.name,”dining room”); Dining.length.feet=13; Dining.length.inches=6.5; Dining.width.feet=20; Dining.width.inches=10;

6 Initializing Nested Structures
In the previous example we can initialize the nested structure such that each structure of type Distance, which is embedded in Room, is initialized separately. Which involves surrounding the values with braces and separating them with commas. Example; Room dining ={“Dining room”, {13, 6.5},{20,10} };

7 Example struct Distance { int feet; float inches; }; struct Room char name[20]; Distance length; Distance width; void main( ) { Room dining= {“Dining room”, {13,3.2}, {10,0.0} }; cout<<dining.name<<endl; cout<<dining.length.feet<<endl; cout<< dining.length.inches<<endl; cout<< dining.width.feet<<endl; cout<<dining.width.inches<<endl; }

8 Activity Create a structure called Volume that uses three variables of type Distance to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents, and print out the result.

9 Array of Structures

10 Example struct student { int regNo; float gpa ; }; void main ( ) int n; student std[10]; for (n=0; n<10; n++) { cout<< “Enter student Reg#\n”; cin>>std[n].regNo; cout<<“Enter Gpa\n”; cin>>std[n].gpa; } cout<<std[n].regNo<<endl; cout<<std[n].gpa<<endl

11 Accessing Structure Members with pointers to structures

12 Example struct student { char name[64]; char course[128]; int age; int year; }; void main() student s1 = {"Ali", "CS201- Introduction to programming", 22, 2008}; student *sptr; sptr = &s1; cout << "Displaying the structure data members using pointers" << endl; cout << "Using the * operator" << endl; cout << endl; cout << "The name is " << (*sptr).name<<endl; cout << "The course is " <<(*sptr).course<<endl; cout << "The age is " <<(*sptr).age<<endl; cout << "The year is " << (*sptr).year << endl; }

13 Array of Structures with Pointers (Using *. Operator)
struct student { int rollno; int marks; }; void main() { student s[3]; student *p= s; cout << "Enter Record" << endl; for(int i=0;i<3;i++) cin>>(*(p+i)).rollno; cin>>(*(p+i)).marks; } student t; for(int pass=0;pass<3;pass++) for(int j=0;j<2;j++) if (p[j].marks<p[j+1].marks) { t=p[j]; p[j]=p[j+1]; p[j+1]=t; } cout<<"Roll NO\tMarks"<<endl; for( i=0;i<3;i++) cout<<(*(p+i)).rollno<<"\t"; cout<<(*(p+i)).marks<<endl;

14 Array of Structures with Pointers (Using -> Operator)
struct student { int rollno; int marks; }; void main() { student s[3]; student *p= s; cout << "Enter Record" << endl; for(int i=0;i<3;i++) cin>>(p+i)->rollno; cin>>(p+i)->marks; } student t; for(int pass=0;pass<3;pass++) for(int j=0;j<2;j++) if (p[j].marks<p[j+1].marks) { t=p[j]; p[j]=p[j+1]; p[j+1]=t; } cout<<"Roll NO\tMarks"<<endl; for( i=0;i<3;i++) cout<<(p+i)->rollno<<"\t"; cout<<(p+i)->marks<<endl;


Download ppt "Structure As Function Arguments"

Similar presentations


Ads by Google