C * 运算符在数组赋值中的含义
这行是什么意思?我已经好几年没学过C了。它是否执行括号中的操作然后使 int 结果成为指针?
b[0] = *(start + pos++);
What does this line mean? I havn't done C in a few years. Does it perform the operation in parens then make the int result a pointer??
b[0] = *(start + pos++);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然
start
是一个指针(或者是一个数组,无论如何这里都会衰减为指针),这意味着()
中表达式的结果是一个指针,而不是一个int
。*
只是取消引用该指针。整个事情相当于简单的
b[0] = start[pos++]
,但由于某种原因,有些人更喜欢使用您帖子中的混淆形式。Apparently
start
is a pointer (or an array, which will decay to a pointer here anyway), which means that the result of the expression in()
is a pointer, not anint
. The*
simply dereferences that pointer.The whole thing is equivalent to plain
b[0] = start[pos++]
, but for some reason some people prefer to use the obfuscated form as in your post.假设 start 是指针,pos 是整数,它可以很容易地重写为:
希望这有助于您理解。
Assuming start is pointer and pos is an integer, it can be easily rewritten to:
Hope this helps you understand.
为了使这一点有意义,
start
或pos
必须是一个指针;最有可能的是,它会是开始
。然后代码可以写为实际上,即使 pos 是指针,代码也是等效的,因为 C 的数组订阅语义有些有趣。
For this to make sense, either
start
orpos
must be a pointer; most likely, it will bestart
. The code can then be written asActually, the code is equivalent even if
pos
were the pointer because of C's somewhat funny semantics of array subscription.运算符“*”返回运算符后面的指针或表达式所指向的位置处的值。
一些例子:
有时,特别是在嵌入式系统中,这比使用数组表示法更具可读性。示例:
status = *(UART + STATUS_REGISTER_OFFSET);
The operator '*' returns the value at the location pointed to by the pointer or expression following the operator.
Some examples:
Sometimes, especially in embedded systems, this is more readable than using the array notation. Example:
status = *(UART + STATUS_REGISTER_OFFSET);
请注意这些表达式的等效性。首先,假设以下声明建立了正在使用的类型:
位于内存中的
T
类型的对象:现在,可以通过多种方式访问
Note the equivalence of these expressions. First, assume the following declarations establish the types in play:
Now, an object of type
T
located in memory at positioncan be accessed in several ways: