This chapter introduces arrays. Our focus is on declaring arrays, accessing their elements, and passing them as parameters. We introduce arrays with a program that reverses its input and show how to pass arrays as parameters by rewriting this program, building it on top of a pair of user-defined functions. We further illustrate arrays by writing several useful functions: one reads values into an array until it encounters a sentinel, another searches an array for a particular value, and the last reads values into an array using a technique known as insertion sort. The chapter concludes with a case study that makes use of almost all the C features we've seen so far: a program that produces a histogram of its input.
Jump to: [Previous Chapter | Next Chapter]
Array´Â ¼ø¼¸¦ °®´Â µ¿ÀÏÇÑ ÇüÀÇ ÀÚ·áµéÀÇ ¸ðÀÓÀÌ´Ù. (An ordered collection of homogeneous data)
ArrayÀÇ °¢ ¿ä¼Ò¸¦ element¶ó ÇÑ´Ù.
Array´Â element type°ú size·Î Á¤ÀǵȴÙ.
Array´Â (element Çϳª¸¦ ÀúÀåÇÒ ¼ö ÀÖ´Â ¸Þ¸ð¸®) * (array size) ¸¸ÅÀÇ ¸Þ¸ð¸®¸¦ ÇÒ´ç¹Þ´Â´Ù.
Array name (¹è¿¸í)Àº ÇÒ´ç¹ÞÀº ¸Þ¸ð¸®ÀÇ ½ÃÀÛ ÁÖ¼Ò¿Í °°´Ù.
¿¹) 32 bit compile °æ¿ì
int ary[5]; // integer 5°³¸¦ ÀúÀåÇÒ ¼ö ÀÖ´Â ¹è¿ ary Á¤ÀÇ sizeof (int) ==> 4 sizeof (ary) ==> 20
»ç¿ë ¿¹) ÀÔ·ÂÀ» °Å²Ù·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥ (revintp2.c)
ÀÔ·Â: 10 20 18 68 1
Ãâ·Â: 1 68 18 20 10
À§ ÇÁ·Î±×·¥ÀÇ for-loop version: (revint.c)
int ary1[10][10]; // not ary1[10, 10]; double ary2[10][10][10];
func(int ary[]) { . . . } // func´Â 1Â÷¿ø ¹è¿À» parameter·Î »ç¿ë func (int ary[10][10]) { . . . } func(int ary[][10]) { . . . }»ç¿ë ¿¹) ÀÔ·Â ¹ÝÀü ÇÁ·Î±×·¥ÀÇ º¯Çü (revint2.c, tabfill.c)
int ary[5] = { 1, 2, 3, 4, 5 }; int ary[] = { 1, 2, 3, 4, 5 }; char ch[5] = { 'a', 'b', 'c', 'd', 'e' }; int ary[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int ary[][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int ary[3][] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Wrong int ary[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; char ch[10][5] = {{ 'O', 'n', 'e' }, { 'T', 'w', 'o' }};
n = 3 [1] = 10 [2] = 20 [3] = 30 Average = 20
89 87 56 89 67 78 79 80 85 97 100 100 23 45 12 14 87 88 84 84 84 84 77 77 72 73 68 69 69 100 100 11 75 71 71 72 79 84 71 72 70 74 75 73 71 79 72 55 55 68 65 67 72 71 75 73 71 74 71 71 78 79 80 100 91 92 66 61 61 57 57 65 71 74 78 78 78 78 78 -1Ãâ·Â ¿¹)
0- 9 | 10- 19 |** (3) 20- 29 |* (1) 30- 39 | 40- 49 |* (1) 50- 59 |*** (5) 60- 69 |******* (11) 70- 79 |************************* (37) 80- 89 |******** (13) 90- 99 |** (3) 100-100 |*** (5)