用于指向一行二维数组的C指针声明

Ank*_*wal 3 c

我在第269页的KN King的书中看到了这个宣言

int a[ROWS][COLS], (*p)[COLS];

p = &a[0];
Run Code Online (Sandbox Code Playgroud)

p现在指向第一行的二维数组.我理解为什么a[0]指向第一排的二维数组.但我不明白声明的语法p.这是什么意思,我怎么记得它?

做什么的parens *p(*p)这个语法在运算符优先级方面意味着什么?

Mik*_*ike 16

> "But I do not understand the syntax for declaring p"

所以p声明为:

int (*p)[COLS];
Run Code Online (Sandbox Code Playgroud)

它是指向大小的ints 数组的指针COLS.

> "What does that mean and how do I remember it?"

以下是你如何判断,使用螺旋规则并从()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
Run Code Online (Sandbox Code Playgroud)

当然,你总是可以作弊来得到答案:

在此输入图像描述

> "what does this syntax mean in terms of operator precedence?"

C语言中,[]优先于一元 *,这意味着你需要()为了p成为一个指向ints数组的指针,而不是一个指向ints 的指针数组.