c语言拾遗录之数组

    xiaoxiao2021-03-25  53

    1、数组名的本质 数组名的本质相当于一个常量指针 如:int a[10] a<=>int *const a; int b[10][20] b<=>int (*const b)[20] 所以a[5]==*(a+5);    b[8][9]==*(*(b+8)+9)==*(b+8)[9]; 2、数组在函数中传参时会被降级为指针

    3、数组大小

    #include<iostream> using namespace std; int main() { int arr[] = { 1, 2, 3, 4 }; int *ptr = (int *)(&arr + 1);//ptr指向数组后的一个一片四字节空间 cout << *(arr + 1) << endl;//2此时的arr代表数组首元素的首地址 cout << *(ptr - 1) << endl;//4,&arr代表整个数组的首地址 cout << sizeof(arr[100]) << endl;//arr[100]不会出错,因为sizeof是关键字,求值是在编译的时候所以不会出错 //&arr与&arr[0]代表不同的含义,&arr整个数组的首地址,&arr[0]数组首元素的地址 cout << &arr[0] << endl;//0100FCB0 cout << &arr[1] << endl; //0100FCB4数组的元素a[0]在低地址, a[n]在高地址 cout << sizeof(arr) << endl;//16 int brr[2][3] = { { 1, 2, 3 }, { 3, 4 } }; cout << sizeof(brr) << endl;//24相当于整个数组的大小 cout << brr << endl;//0048FA5C cout << sizeof(*brr) << endl;//12相当于一位数组brr[3]的大小 cout << *brr << endl;//0048FA5C cout << sizeof(&brr) << endl;//4普通指针的大小 cout << &brr << endl;//0048FA5C cout << sizeof(&brr[0]) << endl;//4 cout << &brr[0] << endl;//0048FA5C cout << sizeof(&brr[0][0]) << endl;;//4 cout << &brr[0][0] << endl;//0048FA5C cout << sizeof(brr[0]) << endl;//12相当于一位数组brr[3]的大小 cout << brr[0] << endl;//0048FA5C system("pause"); }

    数组的引用

    #include<iostream> using namespace std; int main() { int arr[5] = { 12, 3, 4 }; int(&a)[5] = arr; cout << a[2] << endl;//2 int &b = arr[0]; cout << b << endl; system("pause"); }

    转载请注明原文地址: https://ju.6miu.com/read-26065.html

    最新回复(0)