的C指针声明指向2-D阵列的一行 [英] C pointer declaration for pointing to a row of 2-d array

查看:105
本文介绍了的C指针声明指向2-D阵列的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个声明在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 ints 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而言意味着什么呢?

C语言中, [] 有超过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 ints, instead of an array of pointers to ints.

这篇关于的C指针声明指向2-D阵列的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆