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).

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]
        <<"\t\ttypeof(a) = "<<abi::__cxa_demangle(typeid(a).name(),0,0,0)<<endl;
	cout<<"a[0]="<<a[0]// conversion to &a[0][0]
        <<"\t\ttypeof(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]
        <<"\t\ttypeof(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
*/

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
}

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("%d\n", a[i]);
}
void g(int (*a)[10]){// pointer to array parameter is not transformed
    for(int i = 0; i < 10; ++i)printf("%d\n", (*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)
}

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++.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值