首页 > 分享 > C语言学习3(数组)

C语言学习3(数组)

一维数组

int a[10]={0,1,2,3,4,5,6};// 后面3个元素默认是0 int a[]={1,2,3,4,5};// ok int a[];// error,不进行初始化且不指定长度是不可以的 (but different types) The address of the array is also the address of its first element. In both C and C++, the address of the array and the address of the first element of the array are the same value. In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. The address of an object is the same as the address of its first member (Their types however, are different). int a[] = {1,2,3}; int (*b)[3] = &a; It is true that a (once decayed), &a and &a[0] should all point to the same address. However,they do not point to the same kind of thing (&a points to an array, while the others point to integers). 1234567891011121314 Multidimensional Array

在这里插入图片描述

// array of 2 arrays of 3 ints each, can be viewed as a 2x3 matrix, with row-major layout int a[2][3] = {{1,2,3},{4,5,6}}; int (*p1)[3] = a; // pointer to the first 3-element row int a[][5]={{0,1,2,3,4},{2,3,4,5,6}};// 第一个花括号给第一行,第二个花括号给第二行 int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};// 列数是必须给出的,行数可以由编译器来数 int a[2][3];//2行3列 void test_two_dimension(){cout<<"a="<<a // conversion to &a[0] <<"tttypeof(a) = "<<abi::__cxa_demangle(typeid(a).name(),0,0,0)<<endl;cout<<"a[0]="<<a[0]// conversion to &a[0][0] <<"tttypeof(a[0]) = "<<abi::__cxa_demangle(typeid(a[0]).name(),0,0,0)<<endl;cout<<"&a[0][0]="<<&a[0][0] <<"ttypeof(&a[0][0]) = "<<abi::__cxa_demangle(typeid(&a[0][0]).name(),0,0,0)<<endl<<endl;cout<<"a[1]="<<a[1]// conversion to &a[1][0] <<"tttypeof(a[1]) = "<<abi::__cxa_demangle(typeid(a[1]).name(),0,0,0)<<endl;cout<<"&a[1][0]="<<&a[1][0] <<"ttypeof(&a[1][0]) = "<<abi::__cxa_demangle(typeid(&a[1][0]).name(),0,0,0)<<endl<<endl;// row-major order(a[0][2] is followed by a[1][0])cout<<"&a[0][2]="<<&a[0][2]<<"t&a[1][0]="<<&a[1][0]<<endl; } output: /* a=0x602270 typeof(a) = int [2][3] a[0]=0x602270 typeof(a[0]) = int [3] &a[0][0]=0x602270 typeof(&a[0][0]) = int* a[1]=0x60227c typeof(a[1]) = int [3] &a[1][0]=0x60227c typeof(&a[1][0]) = int* &a[0][2]=0x602278 &a[1][0]=0x60227c */

12345678910111213141516171819202122232425262728293031323334 Assignment

// Objects of array type are not modifiable lvalues, and although their address may be taken, they cannot appear // on the left hand side of an assignment operator. int main(){ char a[10]; a = "123";// error char b[10] = "dqsl";// ok } 1234567 Implicit conversions

// Array to pointer conversion Any lvalue expression of array type,when used in any context other than as the operand of the address-of operator as the operand of sizeof as the operand of typeof and typeof_unqual (since C23) as the string literal used for array initialization undergoes a conversion to the non-lvalue pointer to its first element. int a[3], b[3][4]; int *p = a; // conversion to &a[0] int (*q)[4] = b; // conversion to &b[0] // When an array type is used in a function parameter list, it is transformed to the corresponding pointer type: // int f(int a[2]) and int f(int *a) declare the same function. // Since the function's actual parameter type is pointer type, a function call with an array argument performs // array-to-pointer conversion; // the size of the argument array is not available to the called function and must be passed explicitly: void f(int a[], int sz){// actually declares void f(int* a, int sz) for(int i = 0; i < sz; ++i)printf("%dn", a[i]); } void g(int (*a)[10]){// pointer to array parameter is not transformed for(int i = 0; i < 10; ++i)printf("%dn", (*a)[i]); } int main(void){ int a[10] = {0}; f(a,10); // converts a to int*, passes the pointer g(&a);// passes a pointer to the array (no need to pass the size) }

123456789101112131415161718192021222324252627 VLA

//variable length array const int n = 5; int m[n]; // 上述代码(在GNU/Linux,CentOS7.9,gcc4.8.5环境下),注意不使用-pedantic-errors则可能不报错 1)CFLAGS+=-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -std=c90 -Wall -pedantic-errors 报错error: ISO C90 forbids variable length array 'm' [-Wvla] 2)CFLAGS+=-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -std=c99 -Wall -pedantic-errors 编译通过,没有警告错误 // Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. 1234567891011

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
https://stackoverflow.com/questions/27880696/gcc-4-8-2-default-compiles-and-runs-variable-length-arrays-fine
https://stackoverflow.com/questions/4159746/variable-length-arrays-in-c89
https://en.cppreference.com/w/c/language/array
https://encyclopedia.thefreedictionary.com/row-major+order
https://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory
https://stackoverflow.com/questions/46047040/2-dimensional-array-name-1st-element
https://stackoverflow.com/questions/20912353/difference-between-a-and-a-in-c-where-a-is-an-array
https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c
https://stackoverflow.com/questions/25008826/what-is-the-difference-between-a-a0-a-in-c
https://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499
https://arjunsreedharan.org/post/69303442896/how-to-find-the-size-of-an-array-in-c-without
https://c-faq.com/aryptr/aryvsadr.html
https://eng.libretexts.org/Courses/Delta_College/C___Programming_I_(McClanahan)/12%3A_Pointers/12.04%3A_Arrays%2C__Pointers_and_Such/12.4.01%3A_Pointer_to_an_Array_-_Array_Pointer
https://itecnote.com/tecnote/c-why-is-arr-and-arr-the-same/
https://stackoverflow.com/questions/18361111/are-a-a-a-a0-a0-and-a00-identical-pointers
http://www.torek.net/torek/c/pa.html
https://people.iith.ac.in/rogers/pds_theory/lect21.pdf

相关知识

c语言数组初始化——int a[N] = {0};
c语言数组初始化——int a[N] = {0}的探究
入门c语言(二)
数组退化
用c语言写一朵最简单的花
2023年汉江师范学院普通专升本《C语言程序设计》考试大纲
机器学习术语表:语言评估
【未来虫教育】C语言编程时90%的小伙伴常犯的17种错误!
3012C语言
C语言:输出所有的水仙花数

网址: C语言学习3(数组) https://m.huajiangbk.com/newsview662029.html

所属分类:花卉
上一篇: 当代艺术 x 太空科技 x 定制
下一篇: 花妖传说