使用type_t arr_name[const_n]
创建数组
type_t
是类型
,const_n
是常量表达式
int arr[10];
char arr2[10];
double arr3[5];
double arr4[2+3];
创建数组
的同时初始化
数组
int arr[10] = {1};//第一个元素初始化为1,其余的元素默认初始化为0
char ch1[10] = { 'a','b','c'};//a b c 0 0 0 0 0 0 0
char ch2[10] = "abc"; //a b c 0 0 0 0 0 0 0
注意"abc"
指定了a,b,c,\0
四个字符,{ 'a','b','c'}
只有三个.其余全是\0
.因为[10]
.
char ch3[] = "abc";
char ch4[] = { 'a', 'b', 'c' };
char ch5[10] = "abc";
注意ch3
大小4,ch4
大小3.
97是a,0就是\0. 注意'0'是字符零,ASCII码是48
越界
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
//char arr[] = "";
//scanf("%s", arr);
//printf("%s\n", arr);
int arr[] = { 0 };//1
int i = 0;
for (i = 0; i < 10; i++)
{
arr[i] = i;
}
通过下标引用[]
访问数组元素
int main()
{
int arr[20] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
//0~9
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);//arr[i]表示数组的一个元素,而不是创建数组
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 0 0 0 0 0 0 0 0
其中数组大小
用int sz = sizeof(arr) / sizeof(arr[0]);
表示.
数组在内存连续存放
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10};
//0~9
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
printf("&arr[%d] = %p\n", i, &arr[i]);
}
//int arr[40960] = {0};
return 0;
}
&arr[0] = 0113FD90
&arr[1] = 0113FD94
&arr[2] = 0113FD98
&arr[3] = 0113FD9C
&arr[4] = 0113FDA0
&arr[5] = 0113FDA4
&arr[6] = 0113FDA8
&arr[7] = 0113FDAC
&arr[8] = 0113FDB0
&arr[9] = 0113FDB4
地址随数组下标递增
(这是虚拟内存,使用时有对应的物理地址)类型
个字节 char arr[10] = { 1,2,3,4,5,6,7,8,9,10};
&arr[0] = 004FF820
&arr[1] = 004FF821
&arr[2] = 004FF822
&arr[3] = 004FF823
&arr[4] = 004FF824
&arr[5] = 004FF825
&arr[6] = 004FF826
&arr[7] = 004FF827
&arr[8] = 004FF828
&arr[9] = 004FF829
int arr[40960] = {0};
严重性 代码 说明 项目 文件 行 禁止显示状态 警告 C6262 函数使用堆叠的 "163840" 字节。请考虑将一些数据移动到堆。 数组 C:\Users\yangcongxiaoyou\Desktop\CppPractice\C初阶\数组\源.cpp 80
太大
去堆区
1Byte(Byte 字节) = 8Bit
1KB (Kilobyte 千字节) = 1024Byte,
1MB (Megabyte,兆字节,简称“兆”) = 1024KB,
1GB (Gigabyte,吉字节,又称“千兆”) = 1024MB,
1TB (Terabyte,太字节,或百万兆字节) = 1024GB,其中1024=2^10 ( 2 的10次方)。
二维是行开头
int arr[3][4] = {1,2,3,4,5};
int arr2[3][4] = { {1,2},{3,4},{5} };
double arr1[5][6];
必须知道一行多少个
)int arr[][4] = { 1,2,3,4,5 };
使用[i][j]
访问二维数组.
int arr[3][4] = { {1,2},{3,4},{5} };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
1 2 0 0
3 4 0 0
5 0 0 0
int arr[3][4] = { {1,2},{3,4},{5} };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("&arr[%d][%d] = %p\n", i, j, &arr[i][j]);
}
printf("\n");
}
&arr[0][0] = 012FFA30
&arr[0][1] = 012FFA34
&arr[0][2] = 012FFA38
&arr[0][3] = 012FFA3C
&arr[1][0] = 012FFA40
&arr[1][1] = 012FFA44
&arr[1][2] = 012FFA48
&arr[1][3] = 012FFA4C
&arr[2][0] = 012FFA50
&arr[2][1] = 012FFA54
&arr[2][2] = 012FFA58
&arr[2][3] = 012FFA5C
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
for (i = 0; i <= 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 -858993460
这里i <= 10
,出现越界访问.编译器不一定
能检查出错误.
数组名是指针
,&arr[0]等于arr
.
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
//int* p = &arr[0];
int* p = arr;
for (int i = 0; i < 10; i++)
{
printf("%p = %d = %p = %d = %p = %d\n", p + i, *(p + i), &arr[i], arr[i], arr + i, *(arr + i));
}
return 0;
}
00AFFE58 = 1 = 00AFFE58 = 1 = 00AFFE58 = 1
00AFFE5C = 2 = 00AFFE5C = 2 = 00AFFE5C = 2
00AFFE60 = 3 = 00AFFE60 = 3 = 00AFFE60 = 3
00AFFE64 = 4 = 00AFFE64 = 4 = 00AFFE64 = 4
00AFFE68 = 5 = 00AFFE68 = 5 = 00AFFE68 = 5
00AFFE6C = 6 = 00AFFE6C = 6 = 00AFFE6C = 6
00AFFE70 = 7 = 00AFFE70 = 7 = 00AFFE70 = 7
00AFFE74 = 8 = 00AFFE74 = 8 = 00AFFE74 = 8
00AFFE78 = 9 = 00AFFE78 = 9 = 00AFFE78 = 9
00AFFE7C = 10 = 00AFFE7C = 10 = 00AFFE7C = 10
加法交换律
arr[i] = *(arr + i) = *(i + arr) = i[arr]
;int* arr
或者int arr[]
,都表示传入数组名字(指针)
sizeof(arr)
是指整个
数组;&arr
是取整个
数组地址. int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
printf("%p\n", arr);
printf("%p\n", arr + 1);
printf("%p\n", &arr[0]);
printf("%p\n", &arr[0] + 1);
printf("%p\n", &arr);
printf("%p\n", &arr + 1);
012FFDAC
012FFDB0
012FFDAC
012FFDB0
012FFDAC
012FFDD4
012FFDD4-012FFDAC=40
012FFDB0-012FFDAC=4