数组和绑定问题。 [英] Array and binding question.

查看:59
本文介绍了数组和绑定问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>来自p112(K& R)。

给定一个数组声明为

static char arr [2] [13] = {{0,1, ........},{0,1,.....}};

让arr作为参数传递给f。


f(int(* arr)[13]){....}


注意括号是必要的,否则它将是

解释为


int * arr [13](13个指向整数的指针数组,而不是

指向13个整数数组的指针这个

解释的基础是[]绑定比*更严格。


现在,已经进行了广泛的讨论关于优先级和

关联性....但我的问题是这个。


RH已经描述了优先级和关联性决定哪个

运算符与哪些操作数相关联。


在上面的例子中,我假设运算符是[],但是什么是

操作数?

或者我错过了这里别的东西......可能。


谢谢

>From p112 ( K&R).
Given an array declared as
static char arr[2][13]= { { 0,1,........},{0,1,.....}};
let arr be passed as an argument to f.

f( int (*arr)[13] ) {....}

It is noted that the parentheses are necessary else it would be
interpreted as

int *arr[13] ( an array of 13 pointers to integers, as opposed to the
intended pointer to an array of 13 integers. The basis of this
explanation is that "[ ]" bind tighter than " * ".

Now, there have been extensive discussions about precedence and
associativity ....but my question is this.

RH has described precedence and associativity as deciding which
operators are associated with which operands.

In the case above, I assume the operator is the "[]", but what are
operands?
Or am I missing something else here...probably.

Thanks

推荐答案

mdh< md **@comcast.netwrites:
mdh <md**@comcast.netwrites:

>>来自p112(K& R)。
>>From p112 ( K&R).



给定一个数组声明为

static char arr [2] [13] = {{0,1,..... ...},{0,1,.....}};


让arr作为参数传递给f。


f(int(* arr)[13]){....}


注意括号是必要的,否则它将是

解释为


int * arr [13](13个指向整数的指针数组,而不是

指向13个整数数组的指针。这个

解释的基础是[]绑定比*更严格。


现在,已经进行了广泛的讨论优先级和

关联性....但我的问题是这个。


RH已经将优先级和关联性描述为决定哪个

运算符与哪些操作数相关联。


在上面的例子中,我假设运算符是[],但是什么是

操作数?

或者我错过了什么否则在这里......可能。

Given an array declared as
static char arr[2][13]= { { 0,1,........},{0,1,.....}};
let arr be passed as an argument to f.

f( int (*arr)[13] ) {....}

It is noted that the parentheses are necessary else it would be
interpreted as

int *arr[13] ( an array of 13 pointers to integers, as opposed to the
intended pointer to an array of 13 integers. The basis of this
explanation is that "[ ]" bind tighter than " * ".

Now, there have been extensive discussions about precedence and
associativity ....but my question is this.

RH has described precedence and associativity as deciding which
operators are associated with which operands.

In the case above, I assume the operator is the "[]", but what are
operands?
Or am I missing something else here...probably.



这里没有运算符和操作数,只是语法。类型中使用的*,[]

和()s不是对任何操作的运算符。


原因(* x)[13]意味着什么确实来自

C的语法规则,但因为它们非常冗长,所以通过说出类似的内容来总结语法规则的效果要容易得多。 ; []在一个类型中紧紧绑定* b $ b。但是,您需要记住,这只是

语言规范中写出的正式语法结果的简写。


顺便提一下,表达式也是如此。标准中没有运算符

优先级表,只是一个语法,对于

例子来说,一个加法不能是<的一部分

muliticative-expression,除非它是通过允许一个

cast-expression的规则,它可以是一元表达式,可以是一个

postfix-expression可以是一个primary-expression,它可以是括号中的

表达式!你可以看到为什么摘要表很受欢迎。


-

Ben。

There are no operators and no operands, here, just syntax. The *, []
and ()s used in types are not operators operating on anything.

The reason (*x)[13] means what is does comes from the syntax rules of
C, but because these are very wordy, it is much easier to summarise
the effect of the syntax rules by saying things like "[] binds more
tightly that * in a type". You need to remember, though, that is just
a shorthand for the consequences of a formal grammar, written out in the
language specification.

The same is true, incidentally, for expressions. There is no operator
precedence table in the standard, just a grammar that says, for
example, that an addition can''t be one of the parts of a
muliticative-expression, unless it is via the rule that allows a
cast-expression, which can be a unary-expression, which can be a
postfix-expression which can be a primary-expression, which can be an
expression in brackets! You can see why a summary tables is popular.

--
Ben.


On 7月31日下午5:36,Ben Bacarisse< ben.use ... @ bsb.me.ukwrote:
On Jul 31, 5:36 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:

mdh< m ... @ comcast .netwrites:
mdh <m...@comcast.netwrites:


f(int(* arr)[13]){....}

f( int (*arr)[13] ) {....}


注意括号是必要的,否则它将被解释为
It is noted that the parentheses are necessary else it would be
interpreted as


int * arr [13] ...... snip
int *arr[13] ......snip



没有运算符和操作数,这里,只是语法。类型中使用的*,[]

和()s不是对任何操作的运算符。


原因(* x)[13]意味着什么确实来自

C的语法规则,但因为它们非常冗长,所以通过说出类似的内容来总结语法规则的效果要容易得多。 ; []在一个类型中紧紧绑定* b $ b。但是,您需要记住,这只是

a语言规范中写出的正式语法结果的简写。

-

Ben。
There are no operators and no operands, here, just syntax. The *, []
and ()s used in types are not operators operating on anything.

The reason (*x)[13] means what is does comes from the syntax rules of
C, but because these are very wordy, it is much easier to summarise
the effect of the syntax rules by saying things like "[] binds more
tightly that * in a type". You need to remember, though, that is just
a shorthand for the consequences of a formal grammar, written out in the
language specification.
--
Ben.



谢谢...但是我可以问这个。

接受你所描述的规则,你能否解释一下为什么

一个意思是


指向13个整数数组的指针 -int(* arr)[13]

vs


一个13 ptrs到整数的数组 -int * arr [13]。


我可能会遗漏一些非常基本的东西,因此问题。

虽然绑定的语法和含义;看起来非常明显,每次我需要查看它的事实,告诉我我错过了

的东西。


thanks

Thanks...but may I ask this.
Accepting the rules as you described, could you perhaps explain why
one would mean

"a pointer to an array of 13 integers" -int (*arr)[13]

vs

"an array of 13 ptrs to integers" -int *arr[13].

I might be missing something quite basic here, hence the question.
Although the syntax and meaning of "binding" seem quite obvious, the
fact that I need to look it up each time, tells me I am missing
something.

thanks


mdh写道:
mdh wrote:

7月31日下午5:36,Ben Bacarisse< ben .use ... @ bsb.me.ukwrote:
On Jul 31, 5:36 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:

> mdh< m ... @ comcast.netwrites:
>mdh <m...@comcast.netwrites:

>> f(int(* arr)[13]){....}
请注意,括号是必要的,否则它将被解释as
int * arr [13] ...... snip
>>f( int (*arr)[13] ) {....}
It is noted that the parentheses are necessary else it would be
interpreted as
int *arr[13] ......snip






>这里没有运算符和操作数,只是语法。类型中使用的*,[]
和()不是对任何操作的运算符。

原因(* x)[13]意味着什么是来自语法规则的
C,但是因为这些非常冗长,所以通过说[]紧密地绑定*类型中的内容来总结语法规则的效果要容易得多。 。但是,你需要记住,这只是
语言规范中写出的形式语法结果的简写。
-
本。
>There are no operators and no operands, here, just syntax. The *, []
and ()s used in types are not operators operating on anything.

The reason (*x)[13] means what is does comes from the syntax rules of
C, but because these are very wordy, it is much easier to summarise
the effect of the syntax rules by saying things like "[] binds more
tightly that * in a type". You need to remember, though, that is just
a shorthand for the consequences of a formal grammar, written out in the
language specification.
--
Ben.



谢谢...但我可以问这个。

接受你所描述的规则,你能解释一下为什么

一个意思是


指向13个整数数组的指针 -int(* arr)[13]

vs


一个13 ptrs到整数的数组 -int * arr [13]。


我可能会遗漏一些非常基本的东西,因此问题。

虽然绑定的语法和含义;看起来非常明显,每次我需要查看它的事实,告诉我我错过了

的东西。


谢谢


Thanks...but may I ask this.
Accepting the rules as you described, could you perhaps explain why
one would mean

"a pointer to an array of 13 integers" -int (*arr)[13]

vs

"an array of 13 ptrs to integers" -int *arr[13].

I might be missing something quite basic here, hence the question.
Although the syntax and meaning of "binding" seem quite obvious, the
fact that I need to look it up each time, tells me I am missing
something.

thanks



我认为指向数组的指针的原始语法(或函数指针,

) )是彻头彻尾的邪恶。如果你需要经常查找,

也许适用于那些阅读你的代码的人...这意味着他们不会。


对我来说,救赎来自typedef。

如果

int a [13];

表示13个整数的数组,那么

typedef int atype [13];

将atype定义为13 int的数组类型。

指向13 int的数组的指针是输入atype *。

它比较有字,但更具可读性。


- 方舟

I think the raw syntax for a pointer to an array (or a function pointer,
for that matter) is outright evil. If you need to look it up often, the
same perhaps applies to those who read your code... which means they won''t.

To me, the salvation comes as typedef.
If
int a[13];
means array of 13 ints, then
typedef int atype[13];
defines atype as a type of array of 13 ints.
A pointer to an array of 13 ints is then of type atype*.
It is wordier but much more readable.

-- Ark


这篇关于数组和绑定问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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