问题描述
有人可以帮助澄清K& R II第99页上的内容。
它说如果pa是一个指针,表达式我使用它下标;
pa [i]与*(pa + i)相同。
这对我来说很困惑。我假设pa [i]指向i th的
地址。对象的元素(我假设的数组)。但是,
则不会*(pa + i)取消引用i th。元素并参考
内容不是地址?
显然我错了,但我想明白为什么?
像往常一样。
Hi,
Could someone help clarify something on page 99 of K&R II.
It says if "pa is a pointer, expressions my use it with a subscript;
pa[i] is identical to *(pa +i)."
Here is what is confusing to me. I assume that pa[i] points to the
address of the "i th " element of an object ( array I assume). But,
then will not *(pa +i) dereference the "i th " element and refer to
the contents not the address?
Obviously I am wrong, but I would like to understand why?
thanks as usual.
推荐答案
当pa [i]出现在右侧时作业的分配
表达式,pa [i]是指内容,而不是地址,只是
,就像*(pa + i)的情况一样。 />
当pa [i]出现在左侧时任务
运算符,例如pa [i] = 7;那么pa [i]指的是
的地址作为分配目的地的元素,就像
*(pa + i)的情况一样上下文。
这两个是相同的:
x = pa [i] + 5;
x = *(pa + i)+ 5
这两个是相同的:
pa [i] = x + 5;
*(pa + i)= x + 5;
但在第一组中,pa [i]和*(pa + i)参考内容,并在第二组中使用
,pa [i]和*(pa + i)告诉编译器将值放入哪个位置
。 />
-
重要的是要记住,在法律方面,计算机
永远不会复制,只有人类才能复制。计算机给出了
命令,而非许可。只有人才能获得许可。
- Brad Templeton
When pa[i] appears on the "right hand side" of an assignment
expression, pa[i] refers to the contents, not to the address, just
as is the case for *(pa + i) .
When pa[i] appears on the "left hand side" of an assignment
operator, such as pa[i] = 7; then pa[i] refers to the address of
the element as the destination for the assignment, just as is
the case for *(pa+i) in the same context.
These two are the same as each other:
x = pa[i] + 5;
x = *(pa+i) + 5
These two are the same as each other:
pa[i] = x + 5;
*(pa+i) = x + 5;
but in the first group, pa[i] and *(pa+i) refer to content, and
in the second group, pa[i] and *(pa+i) tell the compiler which location
to put the value into.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
您的错误是pa [i]是一个对象,而不是它的地址。是(pa + i)
指向对象而*(pa + i)是对象本身。
再读一次,这次来自第97页。
-
Joe Wright
所有事情都应尽可能简单,但并不简单。
--- Albert Einstein ---
Your error is that pa[i] is an object, not its address. It is (pa + i)
that points to the object and *(pa + i) is the object itself.
Read it again, this time from page 97.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
当它们相同时,它们就意味着它。试试这个:
int foo(int i,int pa []){return i [pa]; }
毕竟那只是*(i + pa) - 与*(pa + i)相同。
他们可以当然已经强加了限制,''[]''前面的表达式必须是一个指针,但是当他们定义语言或他们定义语言时他们忘了它只是没有打扰。
-
问候,
Hallvard
And when they these are identical, they mean it. Try this:
int foo(int i, int pa[]) { return i[pa]; }
After all that''s just *(i + pa) - which is the same as *(pa + i).
They could of course have imposed the restriction that the expression
in front of ''[]'' must be a pointer, but either they forgot it when
they defined the language or they just didn''t bother.
--
Regards,
Hallvard
这篇关于帮助指针讨论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!