• 发表于 3年前
• 阅读 4
• 收藏 0
• 评论 0

1. 数组的标志是 []:
``````#include <stdio.h>

int main(void)
{
int nums[3];

nums[0] = 11;
nums[1] = 22;
nums[2] = 33;

printf("%d, %d, %d", nums[0], nums[1], nums[2]);

getchar();
return 0;
}

``````

2. 数组的大小和维数:
``````#include <stdio.h>

int main(void)
{
int nums[10];

printf("数组大小是: %d\n", sizeof(nums));
printf("数组维数是: %d\n", sizeof(nums)/sizeof(nums[0]));
getchar();
return 0;
}

``````

3. 遍历数组:
``````#include <stdio.h>

int main(void)
{
int nums[10];

int i;
for (i = 0; i < sizeof(nums)/sizeof(nums[0]); i++)
nums[i] = i * i;

for (i = 0; i < sizeof(nums)/sizeof(nums[0]); i++)
printf("%d\n", nums[i]);

getchar();
return 0;
}

``````

``````#include <stdio.h>

int main(void)
{
char cs[] = "ABCDEFG";
int i;
for (i = 0; cs[i]; i++) {   /* cs[i] 为假时, 就到了那个空字符了 */
printf("%c\n", cs[i]);
}

getchar();
return 0;
}

``````

4. 对一维数组, 可以不指定维数, 它能自动识别:
``````#include <stdio.h>

int main(void)
{
double ds[] = {1.1, 2.2, 3.3, 4.4};

int count = sizeof ds / sizeof ds[0];
int i;

for (i = 0; i < count; i++) {
printf("%.1f\n", ds[i]);
}

getchar();
return 0;
}

``````

4. 不指定维数的数组常常会用于字符串:
``````#include <stdio.h>

int main(void)
{
char str1[] = "Builder";

/* 假如不怕麻烦可以这样初始化 */
char str2[8] = {'B','u','i','l','d','e','r','\0'};

unsigned i;
for (i = 0; i < sizeof str1/sizeof str1[0]; i++) {
printf("%c", str1[i]);
}

printf("\n%s", str1);
printf("\n%s", str2);

getchar();
return 0;
}

``````

5. 二维数组:
``````#include <stdio.h>

int main(void)
{
int nums[3][4] = {
{11,12,13,14},
{21,22,23,24},
{31,32,33,34}
};

printf("%d,%d,%d\n", nums[0][2], nums[1][2], nums[2][2]);

getchar();
return 0;
}

``````

5. 可以并只能省略第一个维数:
``````#include <stdio.h>

int main(void)
{
int nums[][4] = {
{11,12,13,14},
{21,22,23,24},
{31,32,33,34}
};

printf("%d,%d,%d\n", nums[0][2], nums[1][2], nums[2][2]);

getchar();
return 0;
}

``````

6. 多维数组:
``````#include <stdio.h>

int main(void)
{
int nums[2][3][4] = {
{
{111,112,113,114},
{121,122,123,124},
{131,132,133,134}
},
{
{211,212,213,214},
{221,222,223,224},
{231,232,233,234}
}
};

printf("%d,%d,%d\n", nums[0][0][0], nums[1][1][1], nums[1][2][2]);
getchar();
return 0;
}

``````

7. 多维数组也是可以并只能省略第一个维数:
``````#include <stdio.h>

int main(void)
{
int nums[][3][4] = {
{
{111,112,113,114},
{121,122,123,124},
{131,132,133,134}
},
{
{211,212,213,214},
{221,222,223,224},
{231,232,233,234}
}
};

printf("%d,%d,%d\n", nums[0][0][0], nums[1][1][1], nums[1][2][2]);
getchar();
return 0;
}

``````

8. 字符串数组:
``````#include <stdio.h>

int main(void)
{
char css[][10] = {
"AAA",
"BBB",
"CCCCCCC"
};
size_t i;
for (i = 0; i < sizeof(css)/sizeof(css[0]); i++) {
printf("%s\n", css[i]);
}

getchar();
return 0;
}

``````

9. 没有初始化的局部数组变量, 包含的是一堆垃圾值:
``````#include <stdio.h>

int ns1[10];     /* 这个会初始化为空 */

int main(void)
{
int ns2[10]; /* 这个不会初始化 */

int i;

for (i = 0; i < sizeof(ns1)/sizeof(ns1[0]); i++)
printf("%d\n", ns1[i]);

printf("------------\n");

for (i = 0; i < sizeof(ns2)/sizeof(ns2[0]); i++)
printf("%d\n", ns2[i]);

getchar();
return 0;
}

``````

10. 初始化数组为空其实很简单:
``````#include <stdio.h>

int main(void)
{
int ns1[10] = {NULL}; /* 或 {0} */
int ns2[2][3][4] = {0};

int i;
for (i = 0; i < sizeof(ns1)/sizeof(ns1[0]); i++)
printf("%d\n", ns1[i]);

printf("---------\n");
printf("%d\n", ns2[1][2][3]);

getchar();
return 0;
}

``````

×