|
|
1.1 ! root 1: ! 2: ! 3: array Definition array ! 4: ! 5: ! 6: ! 7: ! 8: An array is a concatenation of data elements, all of which are of ! 9: the same type. All the elements of an array are stored consecu- ! 10: tively in memory, and each element within the array can be ad- ! 11: dressed by the array name plus a subscript. ! 12: ! 13: For example, the array int foo[3] has three elements, each of ! 14: which is an iinntt. The three iinntts are stored consecutively in ! 15: memory, and each can be addressed by the array name ffoooo plus a ! 16: subscript that indicates its place within the array, as follows: ! 17: foo[0], foo[1], and foo[2]. Note that the numbering of elements ! 18: within an array always begins with `0'. ! 19: ! 20: Arrays, like other data elements, may be automatic (aauuttoo), ! 21: static, or external (eexxtteerrnn). ! 22: ! 23: Arrays can be multi-dimensional; that is to say, each element in ! 24: an array can itself be an array. To declare a multi-dimensional ! 25: array, use more than one set of square brackets. For example, ! 26: the multi-dimensional array foo[3][10] is a two-dimensional array ! 27: that has three elements, each of which is an array of ten ! 28: elements. The second sub-script is always necessary in a multi- ! 29: dimensional array, whereas the first is not. For example, ! 30: foo[][10] is acceptable, whereas foo[10][] is not. The first ! 31: form is an indefinite number of ten-element arrays, which is cor- ! 32: rect C, whereas the second form is ten copies of an indefinite ! 33: number of elements, which is illegal. ! 34: ! 35: You can initialize automatic arrays and structures, provided that ! 36: you know the size of the array, or of any array contained within ! 37: a structure. An automatic array is initialized in the same man- ! 38: ner as aggregate, but initialization is performed on entry to the ! 39: routine at run time, instead of at compile time. ! 40: ! 41: ***** Flexible Arrays ***** ! 42: ! 43: A flexible array is one whose length is not declared explicitly. ! 44: Each has exactly one empty `[ ]' array-bound declaration. If the ! 45: array is multidimensional, the flexible dimension of the array ! 46: must be the _f_i_r_s_t array bound in the declaration; for example: ! 47: ! 48: ! 49: int example1[][20]; /* RIGHT */ ! 50: int example2[20][]; /* WRONG */ ! 51: ! 52: ! 53: The C language allows you to declare an indefinite number of ar- ! 54: ray elements of a set length, but not a set number of array ! 55: elements of an indefinite length. ! 56: ! 57: Flexible arrays occur in only a few contexts; for example, as ! 58: parameters: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: array Definition array ! 70: ! 71: ! 72: ! 73: char *argv[]; ! 74: char p[][8]; ! 75: ! 76: ! 77: as extern declarations: ! 78: ! 79: ! 80: extern int end[]; ! 81: ! 82: ! 83: or as a member of a structure -- usually, though not necessarily, ! 84: the last: ! 85: ! 86: ! 87: struct nlist { ! 88: struct nlist *next; ! 89: char name[]; ! 90: }; ! 91: ! 92: ! 93: ***** Example ***** ! 94: ! 95: The following program initializes an automatic array, and prints ! 96: its contents. ! 97: ! 98: ! 99: main() ! 100: { ! 101: int foo[3] = { 1, 2, 3 }; ! 102: ! 103: ! 104: ! 105: printf("Here's foo's contents: %d %d %d\n", ! 106: foo[0], foo[1], foo[2]); ! 107: } ! 108: ! 109: ! 110: ***** See Also ***** ! 111: ! 112: definitions, struct ! 113: _T_h_e _C _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e, ed. 2, pages 25, 83, 210 ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.