Initializing Arrays As simple variables can be initialized at declaration: int price 0;//0 is initial value ◆Arrays can as well: int children[3]={2,12,1}; Equivalent to following: int children[3]; children[0]2; children[1]=12; children[2]1; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-16
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-16 Initializing Arrays ¨ As simple variables can be initialized at declaration: int price = 0; // 0 is initial value ¨ Arrays can as well: int children[3] = {2, 12, 1}; ¨ Equivalent to following: int children[3]; children[0] = 2; children[1] = 12; children[2] = 1;
Auto-Initializing Arrays If fewer values than size supplied: ◆Fills from beginning Fills "rest"with zero of array base type If array-size is left out Declares array with size required based on number of initialization values ◆Example: intb[]={5,12,11}; Allocates array b to size 3 Copyright006 Pearson Addison-Wesley.All rights reserved. 5-17
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-17 Auto-Initializing Arrays ¨ If fewer values than size supplied: ¨Fills from beginning ¨Fills "rest" with zero of array base type ¨ If array-size is left out ¨Declares array with size required based on number of initialization values ¨Example: int b[] = {5, 12, 11}; ¨Allocates array b to size 3