*********************************************************
原文:http://www.cplusplus.com/doc/tutorial/
*********************************************************
数组
|
int billy [5];
|
int billy [5] = { 16, 2, 77, 40, 12071 };
|
int billy [] = { 16, 2, 77, 40, 12071 };
|
billy[2] = 75;
|
a = billy[2];
|
int billy[5];
// declaration of a new Array
billy[2] = 75;
// access to an element of the Array.
|
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;
|
/
/ arrays example
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
|
12206
|
int jimmy [3][5];
|
jimmy[1][3]
|
char century [100][365][24][60][60];
|
int jimmy [3][5];
// is equivalent to
int jimmy [15];
// (3 * 5 = 15)
|
multidimensional array
|
pseudo-multidimensional array
|
#define WIDTH 5 int jimmy [HEIGHT][WIDTH]; int main () | #define WIDTH 5 int jimmy [HEIGHT * WIDTH]; int main () |
#define HEIGHT 3
|
#define HEIGHT 4
|
void procedure (int arg[])
|
int myarray [40];
|
procedure (myarray);
|
// arrays as parameters
#include <iostream>
using namespace std;
void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] <<
" "
;
cout <<
"/n"
;
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0;
}
|
5 10 15
2 4 6 8 10
|
就像你看到的,第一个参数(int arg[])接受任何元素类型为int的数组,而不管它的长度。由于这个原因我们需要引入第二个参数来告诉函数,我们传给它的作为它第一个参数的数组的长度。这就使for循环知道传来的数组的迭代范围,而不会在输出数组时发生数组越界。
Ibase_type[][depth][depth]
|
void procedure (int myarray[][3][4])
|