指针......再一次 [英] Pointers...once again

查看:61
本文介绍了指针......再一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从K& R开始第5章,它并不容易......但它值得

它...我被反复告知!


K& R在声明中表示

p =& c;


p被称为指向c。


我知道这听起来很愚蠢,但是没有说明如何阅读声明

...


你说P等于alpha c吗?或者你只是按照K& R说的方式阅读它?


同样的道理,如何阅读这个声明?


int * ip;


K& R简单地说/ *** ip是一个指向int ** /


稍微更深一点的指针注意(我希望)


引用"一元经营者*和&绑定比

算术运算符更紧密所以赋值


y = * ip + 10等等

这是否意味着在上面的表达中,使用


* ip或ip *没什么区别?


最后,K& R国家像++和*这样的一元运算符关联到

左边。


他们使用的例子是(* ip)++。我不太明白如果不用括号,那么ip会增加。任何人都可以点亮这个




再次,像往常一样,提前感谢任何启蒙。

解决方案

文章< 11 ********************** @ e3g2000cwe.googlegroups。 com>,

mdh< md ** @ comcast.netwrote:


> K& R在声明中说出来/> p =& c;

" p被称为指向c。

我知道这听起来很愚蠢,但没有说它在哪里说如何阅读声明

你说P等于alpha c吗?或者你只是按照K& R所说的方式阅读它?



我会说p等于&符号c (它不是阿尔法)。


>同样的道理,如何阅读此声明?

int * IP;



" int star p"


> Quote"一元经营者*和&绑定比
算术运算符更紧密。所以作业

y = * ip + 10等等

这是否意味着在上面的表达中,使用

* ip或ip *没有区别?



否,因为*是前缀运算符。


>最后,K& R state" ;像++和*这样的一元运算符左右联系。

他们使用的例子是(* ip)++。我不太明白如果没有括号,ip会增加。任何人都可以对此提出一些启示。



根据最近的讨论,我建议在标准中阅读

语法,而不是试图理解

关联性。


- Richard


很多问题,呃? :-)


首先要做的事情。 =运算符表示取,而不是等于。因此


p =& c


不是p等于c。这意味着p取c的地址,或者在其他

的单词中指向c。 p等于c将是


p == c


关于你的第二个问题,K& R中的分析有助于

理解int *,int **,int(*)等的含义。另外,

与* p和p *有很大的不同。更仔细地阅读本书,它将会澄清。


现在,关于表达式:


( * p)++

* p ++

++ * p


您应该考虑运算符优先级。一元运算符++和*

都具有相同的优先级,因此决定它们运行的​​顺序的是它们在操作数中的位置。所以表达式


* p ++


首先递增指针p,然后对其进行解释(第一个

运算符评估是++)。另一方面,表达式


++ * p


首先引用指针然后递增其值(在其他

字,它增加p)指向的对象。


您可以重写这些表达式并得到以下等价物:


* p ++ - (p ++,* p)

++ * p - * p + = 1


更清楚了吗?

mdh escreveu:


>

从K& R开始第5章,它并不容易......但它值得

它...我被反复告知!


(...)


" MDH" < md ** @ comcast.netwrites:


从K& R开始第5章,它并不容易......但值得

it ...我被反复告知了!


K& R在声明中说明了

p =& c;


" p被称为指向c。


我知道这听起来很愚蠢,但没有说明声明如何<读取
...


你说P等于alpha c吗?或者你只是按照K& R所说的方式阅读它?



你说p是指向c的指针。或者p是c的地址。更多

一般来说,p是指向char的指针。在给出地址

c之后,它是指向c的指针。


>

出于同样的原因,如何阅读此声明?


int * ip;


K& R简单地说/ *** ip是指向int ** /



的指针ip是指向整数的指针。


稍微深一点(我希望)


引用"一元经营者*和&绑定比

算术运算符更紧密所以赋值


y = * ip + 10等等


这是否意味着在上面的表达中,使用


* ip或ip *没有区别?



不是。它们是不同的东西。


>

最后,K& R州像++和*这样的一元运算符关联到

左。


他们使用的例子是(* ip)++ 。我不太明白如果不用括号,那么ip会增加。任何人都可以在这上面点亮一些光。




* ip ++返回指针ip指向的值。然后ip是增加后的
。在调试器中粘贴一些printfs或玩游戏

命令行。你会得到的!


祝你好运。


>

再次像往常一样,提前感谢任何启蒙。



-


Starting Chapter 5 in K&R, and it does not get easier..but it is worth
it...I am repeatedly told!

K&R say that in the statement
p=&c;

"p is said to "point to c".

I know this will sound dumb, but no where does it say how the statement
is read...

Do you say "P equals alpha c" or do you read it just the way K&R say?

By the same token, how does one read this declaration?

int *ip;

K&R simply says /*** ip is a pointer to int **/

On a slightly more deeper note ( I hope)

Quote " the unary operators * and & bind more tightly then the
arithmetic operators" so the assignment

y = *ip + 10 etc etc
Does this mean that in the above expression, using

*ip or ip* would make no difference?

Lastly, K&R state "unary operators like ++ and * associate right to
left.

The example they use is (*ip)++. I do not quite understand how without
the parentheses, ip would be incremented. Could anyone throw some light
on this.

Again, as usual, thanks in advance for any enlightenment.

解决方案

In article <11**********************@e3g2000cwe.googlegroups. com>,
mdh <md**@comcast.netwrote:

>K&R say that in the statement
p=&c;

"p is said to "point to c".

I know this will sound dumb, but no where does it say how the statement
is read...

Do you say "P equals alpha c" or do you read it just the way K&R say?

I would say "p equals ampersand c" (it''s not an alpha).

>By the same token, how does one read this declaration?

int *ip;

"int star p"

>Quote " the unary operators * and & bind more tightly then the
arithmetic operators" so the assignment

y = *ip + 10 etc etc
Does this mean that in the above expression, using

*ip or ip* would make no difference?

No, because * is a prefix operator.

>Lastly, K&R state "unary operators like ++ and * associate right to
left.

The example they use is (*ip)++. I do not quite understand how without
the parentheses, ip would be incremented. Could anyone throw some light
on this.

Based on recent discussions here, I would recommend reading the
grammar in the standard instead of trying to understand it in terms of
associativity.

-- Richard


Lot''s of questions, uh? :-)

First things first. The = operator means "takes", not "equals". Hence

p = &c

is not p equals c. It means p takes the address of c or, in other
words, points to c. p equals c would be

p == c

As to your second question, there is an analysis in K&R that helps
understand the meaning of int*, int**, int(*) and so forth. Also,
there''s a big difference from *p and p*. Read the book more careful, it
will be clarifying.

Now, concerning the expressions:

(*p)++
*p++
++*p

You should think of operator precedence. Both unary operators ++ and *
have equal precedence, so what determines the order in which they
operate is their placement among the operands. So the expression

*p++

first increments the pointer p and then derreferences it (the first
operator evaluated is ++). On the other hand, the expression

++*p

first derreferences the pointer and then increments its value (in other
word, it increments the object pointed by p).

You could rewrite those expressions and get the following equivalence:

*p++ --(p++, *p)
++*p --*p += 1

Is that morer clear?
mdh escreveu:

>
Starting Chapter 5 in K&R, and it does not get easier..but it is worth
it...I am repeatedly told!

(...)


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

Starting Chapter 5 in K&R, and it does not get easier..but it is worth
it...I am repeatedly told!

K&R say that in the statement
p=&c;

"p is said to "point to c".

I know this will sound dumb, but no where does it say how the statement
is read...

Do you say "P equals alpha c" or do you read it just the way K&R say?

You say "p is a pointer to c". Or "p is the address of c". More
generally, "p is a pointer to a char". After it is given the address of
c then "it is a pointer to c".

>
By the same token, how does one read this declaration?

int *ip;

K&R simply says /*** ip is a pointer to int **/

ip is a pointer to an integer.

On a slightly more deeper note ( I hope)

Quote " the unary operators * and & bind more tightly then the
arithmetic operators" so the assignment

y = *ip + 10 etc etc
Does this mean that in the above expression, using

*ip or ip* would make no difference?

No. They are different things.

>
Lastly, K&R state "unary operators like ++ and * associate right to
left.

The example they use is (*ip)++. I do not quite understand how without
the parentheses, ip would be incremented. Could anyone throw some light
on this.

*ip++ returns the value pointed to by the pointer ip. ip is then
post-incremented. Stick some printfs in or play around at a debugger
command line. You''ll get it!

best of luck.

>
Again, as usual, thanks in advance for any enlightenment.

--


这篇关于指针......再一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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