问题描述
我碰到这个声明在KN国王的书来到269页
I came across this declaration in KN King's book on Page 269
int a[ROWS][COLS], (*p)[COLS];
p = &a[0];
P
现在指向2维数组的第一行。我明白了为什么 A [0]
点,2-D数组的第一行。但我不明白的语法声明 P
。这是什么意思?我怎么记得?
p
now points to the first row of 2-d array. I understand why a[0]
points to first row of 2-d array. But I do not understand the syntax for declaring p
. What does that mean and how do I remember it?
什么是围绕 * P的括号
在做什么? (* P)
这是什么语法操作符precedence而言意味着什么呢?
What are the parens around *p
doing ? (*p)
what does this syntax mean in terms of operator precedence?
推荐答案
> 但我不明白的语法来声明P
所以 P
被声明为:
int (*p)[COLS];
这是一个指向 INT
所组成的阵列中, COLS
的大小。
It's a pointer to an array of int
s which is COLS
in size.
> 这是什么意思?我怎么记得了?
下面是你如何能知道,并开始由<$ C工作$ C>()取值:
Here's how you can tell, use the spiral rule and start by working in the ()
s:
( p) p
(*p) p is a pointer
(*p)[ ] p is a pointer to an array
int (*p)[ ] p is a pointer to an array of ints
int (*p)[COLS] p is a pointer to an array of ints of size COLS
当然,你总是可以:
&GT; 这是什么语法操作符precedence而言意味着什么呢?
在,这意味着你需要在()
,以便点
是一个指向 INT
秒的阵列,而不是一个指针数组 INT
秒。
In the C Language, []
has precedence over the unary *
, that means you need the ()
in order for p
to be a pointer to an array of int
s, instead of an array of pointers to int
s.
这篇关于的C指针声明指向2-D阵列的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!