|
|
1.1 ! root 1: ! 2: ! 3: initialization Definition initialization ! 4: ! 5: ! 6: ! 7: ! 8: The term initialization refers to setting a variable to its ! 9: first, or initial, value. ! 10: ! 11: ***** Rules of Initialization ***** ! 12: ! 13: Initializers follow the same rules for type and conversion as do ! 14: assignment statements. ! 15: ! 16: If a static object with a scalar type is not explicitly in- ! 17: itialized, it is initialized to zero by default. Likewise, if a ! 18: static pointer is not explicitly initialized, it is initialized ! 19: to NULL by default. If an object with automatic storage duration ! 20: is not explicitly initialized, its contents are indeterminate. ! 21: ! 22: Initializers on static objects must be constant expressions; ! 23: greater flexibility is allowed for initializers of automatic ! 24: variables. These latter initializers can be arbitrary expres- ! 25: sions, not just constant expressions. For example, ! 26: ! 27: ! 28: double dsin = sin(30.0); ! 29: ! 30: ! 31: is a valid initializer, where dsin is declared inside a function. ! 32: ! 33: To initialize an object, use the assignment operator `='. The ! 34: following sections describe how to initialize different classes ! 35: of objects. ! 36: ! 37: ***** Scalars ***** ! 38: ! 39: To initialize a scalar object, assign it the value of a expres- ! 40: sion. The expression may be enclosed within braces; doing so ! 41: does not affect the value of the assignment. For example, the ! 42: expressions ! 43: ! 44: ! 45: int example = 7+12; ! 46: ! 47: ! 48: and ! 49: ! 50: ! 51: int example = { 7+12 }; ! 52: ! 53: ! 54: are equivalent. ! 55: ! 56: ***** Unions and Structures ***** ! 57: ! 58: The initialization of a union by definition fills only its first ! 59: member. ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: initialization Definition initialization ! 70: ! 71: ! 72: ! 73: To initialize a union, use an expression that is enclosed within ! 74: braces: ! 75: ! 76: ! 77: union example_u { ! 78: int member1; ! 79: long member2; ! 80: float member3; ! 81: } = { 5 }; ! 82: ! 83: ! 84: This initializes member1 to five. That is to say, the union is ! 85: filled with an iinntt-sized object whose value is five. ! 86: ! 87: To initialize a structure, use a list of constants or expressions ! 88: that are enclosed within braces. For example: ! 89: ! 90: ! 91: struct example_s { ! 92: int member1; ! 93: long member2; ! 94: union example_u member3; ! 95: }; ! 96: ! 97: ! 98: ! 99: struct example_s test1 = { 5, 3, 15 }; ! 100: ! 101: ! 102: This initializes member1 to five, initializes member2 to three, ! 103: and initializes the first member of member3 to 15. ! 104: ! 105: ***** Strings and Wide Characters ***** ! 106: ! 107: To initialize a string pointer or an array of wide characters, ! 108: use a string literal. ! 109: ! 110: The following initializes a string: ! 111: ! 112: ! 113: char string[] = "This is a string"; ! 114: ! 115: ! 116: The length of the character array is 17 characters: one for every ! 117: character in the given string literal plus one for the null ! 118: character that marks the end of the string. ! 119: ! 120: If you wish, you can fix the length of a character array. In ! 121: this case, the null character is appended to the end of the ! 122: string only if there is room in the array. For example, the ! 123: following ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132: ! 133: ! 134: ! 135: initialization Definition initialization ! 136: ! 137: ! 138: ! 139: char string[16] = "This is a string"; ! 140: ! 141: ! 142: writes the text into the array string, but does not include the ! 143: concluding null character because there is not enough room for ! 144: it. ! 145: ! 146: A pointer to char can also be initialized when the pointer is ! 147: declared. For example: ! 148: ! 149: ! 150: char *strptr = "This is a string"; ! 151: ! 152: ! 153: initializes strptr to point to the first character in This is a ! 154: string. This declaration automatically allocates exactly enough ! 155: storage to hold the given string literal, plus the terminating ! 156: null character. ! 157: ! 158: ***** Arrays ***** ! 159: ! 160: To initialize an array, use a list of expressions that is ! 161: enclosed within braces. For example, the expression ! 162: ! 163: ! 164: int array[] = { 1, 2, 3 }; ! 165: ! 166: ! 167: initializes array. Because array does not have a declared number ! 168: of elements, the initialization fixes its number of elements at ! 169: three. The elements of the array are initialized in the order in ! 170: which the elements of the initialization list appear. For ex- ! 171: ample, array[0] is initialized to one, array[1] to two, and ar- ! 172: ray[2] to three. ! 173: ! 174: If an array has a fixed length and the initialization list does ! 175: not contain enough initializers to initialize every element, then ! 176: the remaining elements are initialized in the default manner: ! 177: static variables are initialized to zero, and other variables to ! 178: whatever happens to be in memory. For example, the following: ! 179: ! 180: ! 181: int array[3] = { 1, 2 }; ! 182: ! 183: ! 184: initializes array[0] to one, array[1] to two, and array[2] to ! 185: zero. ! 186: ! 187: The initialization of a multi-dimensional array is something of a ! 188: science in itself. The ANSI Standard defines that the ranks in ! 189: an array are filled from right to left. For example, consider ! 190: the array: ! 191: ! 192: ! 193: ! 194: ! 195: ! 196: COHERENT Lexicon Page 3 ! 197: ! 198: ! 199: ! 200: ! 201: initialization Definition initialization ! 202: ! 203: ! 204: ! 205: int example[2][3][4]; ! 206: ! 207: ! 208: This array contains two groups of three elements, each of which ! 209: consists of four elements. Initialization of this array will ! 210: proceed from example[0][0][0] through example[0][0][3]; then from ! 211: example[0][1][0] through example[0][1][3]; and so on, until the ! 212: array is filled. ! 213: ! 214: It is easy to check initialization when there is one initializer ! 215: for each ``slot'' in the array; e.g., ! 216: ! 217: ! 218: int example[2][3] = { ! 219: 1, 2, 3, 4, 5, 6 ! 220: }; ! 221: ! 222: ! 223: or: ! 224: ! 225: ! 226: int example[2][3] = { ! 227: { 1, 2, 3 }, { 4, 5, 6 } ! 228: }; ! 229: ! 230: ! 231: The situation becomes more difficult when an array is only parti- ! 232: ally initialized; e.g., ! 233: ! 234: ! 235: int example[2][3] = { ! 236: { 1 }, { 2, 3 } ! 237: }; ! 238: ! 239: ! 240: which is equivalent to: ! 241: ! 242: ! 243: int example[2][3] = { ! 244: { 1, 0, 0 }, { 2, 3, 0 } ! 245: }; ! 246: ! 247: ! 248: As can be seen, braces mark the end of initialization for a ! 249: ``cluster'' of elements within an array. For example, the ! 250: following: ! 251: ! 252: ! 253: int example[2][3][4] = { ! 254: 5, { 1, 2 }, { 5, 2, 4, 3 }, { 9, 9, 5 }, ! 255: { 2, 3, 7 } }; ! 256: ! 257: ! 258: is equivalent to entering: ! 259: ! 260: ! 261: ! 262: COHERENT Lexicon Page 4 ! 263: ! 264: ! 265: ! 266: ! 267: initialization Definition initialization ! 268: ! 269: ! 270: ! 271: ! 272: int example[2][3][4] = { ! 273: { 5, 0, 0, 0 }, ! 274: { 1, 2, 0, 0 }, ! 275: { 5, 2, 4, 3 }, ! 276: ! 277: { 9, 9, 5, 0 }, ! 278: { 2, 3, 7, 0 }, ! 279: { 0, 0, 0, 0 } ! 280: }; ! 281: ! 282: ! 283: The braces end the initialization of one cluster of elements; the ! 284: next cluster is then initialized. Any elements within a cluster ! 285: that have not yet been initialized when the brace is read are in- ! 286: itialized in the default manner. ! 287: ! 288: ***** See Also ***** ! 289: ! 290: array, definitions, struct, union ! 291: ! 292: ! 293: ! 294: ! 295: ! 296: ! 297: ! 298: ! 299: ! 300: ! 301: ! 302: ! 303: ! 304: ! 305: ! 306: ! 307: ! 308: ! 309: ! 310: ! 311: ! 312: ! 313: ! 314: ! 315: ! 316: ! 317: ! 318: ! 319: ! 320: ! 321: ! 322: ! 323: ! 324: ! 325: ! 326: ! 327: ! 328: COHERENT Lexicon Page 5 ! 329: ! 330:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.