Structure Assignments Given structure named CropYield Declare two structure variables: CropYield apples,oranges; Both are variables of"struct type CropYield" Simple assignments are legal: apples oranges; Simply copies each member variable from apples into member variables from oranges Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-11 Structure Assignments ¨ Given structure named CropYield ¨ Declare two structure variables: CropYield apples, oranges; ¨ Both are variables of "struct type CropYield" ¨ Simple assignments are legal: apples = oranges; ¨ Simply copies each member variable from apples into member variables from oranges
Structures as Function Arguments Passed like any simple data type ◆Pass-by-value ◆Pass-by-reference ◆Or combination Can also be returned by function Return-type is structure type Return statement in function definition sends structure variable back to caller Copyright006 Pearson Addison-Wesley.All rights reserved. 6-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-12 Structures as Function Arguments ¨ Passed like any simple data type ¨Pass-by-value ¨Pass-by-reference ¨Or combination ¨ Can also be returned by function ¨Return-type is structure type ¨Return statement in function definition sends structure variable back to caller
Initializing Structures Can initialize at declaration ◆Example: struct Date { int month; int day; int year; }; Date dueDate ={12,31,2003}; Declaration provides initial data to all three member variables Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-13 Initializing Structures ¨ Can initialize at declaration ¨ Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; ¨ Declaration provides initial data to all three member variables