Nested Structures Computer Science Department Nested Structures One

  • Slides: 10
Download presentation
Nested Structures Computer Science Department

Nested Structures Computer Science Department

Nested Structures • One Structure can be nested into another structure. • Example struct

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; }; Computer Science Department

Accessing Nested Structure Members • To access the nested structure members, we have to

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=10; Dining. length. inches=6; Dining. width. feet=20; Computer Science Department

Initializing Nested Structures • In the previous example we can initialize the nested structure

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} }; Computer Science Department

Example struct Distance { int feet; float inches; }; struct Room { char name[20];

Example struct Distance { int feet; float inches; }; struct Room { char name[20]; Distance length; Distance width; }; Computer Science Department 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; }

Array of Structures struct part { int modelnumber; int partnumber; float cost; }; void

Array of Structures struct part { int modelnumber; int partnumber; float cost; }; void main ( ) { int n; part abc[10]; Computer Science Department for (n=0; n<10; n++) { cout<< “Enter Model Number”; cin>>abc[n]. modelnumber; cout<<“Enter Part Number”; cin>>abc[n]. partnumber; cout<<“Enter Cost”; cin>>abc[n]. cost; } }

Passing String as Function Arguments Computer Science Department

Passing String as Function Arguments Computer Science Department

String as Function Arguments void abc(char[ ]); void main () { char str[20]; cin>>str;

String as Function Arguments void abc(char[ ]); void main () { char str[20]; cin>>str; abc(str); } void abc(char str 1[ ]) { cout<<str 1; } Computer Science Department

Passing Array as Function Arguments Computer Science Department

Passing Array as Function Arguments Computer Science Department

Array as Function Argument void output(int[ ]); void main( ) { int arr[10] ={1,

Array as Function Argument void output(int[ ]); void main( ) { int arr[10] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; output(arr); } void output(int arr 1[ ]) { for(int n=0; n<10; n++) cout<<arr 1[n]<<endl; } Computer Science Department