初始化"一个指针整数&QUOT的阵列; [英] Initializing "a pointer to an array of integers"
问题描述
INT(*一)[5];
我们如何初始化一个指针,上述5个整数数组。
是低于前pression是否正确?
INT(*一)[3] = {11,2,3,5,6};
假设你有长度的一个int数组 5
例如
INT X [5];
然后,你可以做 A =安培; X;
INT X [5] = {1};
INT(*一)[5] =&放大器; X;
要访问数组的元素你:(*一)[我]
(== (*(&安培; X))[I]
== (*安培; X)[我]
== X [I]
)括号必要的,因为 []
运算符的precedence高则 *
。 (一个常见的错误可以做 * A [I]
来访问数组的元素)。
明白你在问的问题是一个编译时错误:
INT(*一)[3] = {11,2,3,5,6};
这是不正确的类型不匹配也是如此,因为 {11,2,3,5,6}
可分配给 int类型的[5];
和你分配给 INT(*一)[3]
。
此外,
您可以做一些像一维:
为int *为什么=(INT [2]){1,2};
同样,对于二维试试这个(感谢@ CAF ):
INT(*一)[5] =(INT [] [5]){{1,2,3,4,5},{6,7,8,9 ,10}};
int (*a)[5];
How can we Initialize a pointer to an array of 5 integers shown above.
Is the below expression correct ?
int (*a)[3]={11,2,3,5,6};
Suppose you have an array of int of length 5
e.g.
int x[5];
Then you can do a = &x;
int x[5] = {1};
int (*a)[5] = &x;
To access elements of array you: (*a)[i]
(== (*(&x))[i]
== (*&x)[i]
== x[i]
) parenthesis needed because precedence of []
operator is higher then *
. (one common mistake can be doing *a[i]
to access elements of array).
Understand what you asked in question is an compilation time error:
int (*a)[3] = {11, 2, 3, 5, 6};
It is not correct and a type mismatch too, because {11,2,3,5,6}
can be assigned to int a[5];
and you are assigning to int (*a)[3]
.
Additionally,
You can do something like for one dimensional:
int *why = (int[2]) {1,2};
Similarly, for two dimensional try this(thanks @caf):
int (*a)[5] = (int [][5]){ { 1, 2, 3, 4, 5 } , { 6, 7, 8, 9, 10 } };
这篇关于初始化"一个指针整数&QUOT的阵列;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!