C * 运算符在数组赋值中的含义

发布于 2024-08-14 17:42:52 字数 104 浏览 4 评论 0原文

这行是什么意思?我已经好几年没学过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

以往的大感动 2024-08-21 17:42:52

显然 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 an int. 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.

断念 2024-08-21 17:42:52

假设 start 是指针,pos 是整数,它可以很容易地重写为:

b[0] = start[pos++];

希望这有助于您理解。

Assuming start is pointer and pos is an integer, it can be easily rewritten to:

b[0] = start[pos++];

Hope this helps you understand.

再可℃爱ぅ一点好了 2024-08-21 17:42:52

为了使这一点有意义,startpos 必须是一个指针;最有可能的是,它会是开始。然后代码可以写为

b[0] = start[pos];
pos = pos + 1;

实际上,即使 pos 是指针,代码也是等效的,因为 C 的数组订阅语义有些有趣。

For this to make sense, either start or pos must be a pointer; most likely, it will be start. The code can then be written as

b[0] = start[pos];
pos = pos + 1;

Actually, the code is equivalent even if pos were the pointer because of C's somewhat funny semantics of array subscription.

请别遗忘我 2024-08-21 17:42:52

运算符“*”返回运算符后面的指针或表达式所指向的位置处的值。

一些例子:

value = *pointer;
value = *(pointer);     // Exactly the same as the first statement.
value = *(pointer + 0); // Take contents of pointer and add zero then dereference.
value = *(pointer + 1); // Deference the first location after the location in pointer.

有时,特别是在嵌入式系统中,这比使用数组表示法更具可读性。示例: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:

value = *pointer;
value = *(pointer);     // Exactly the same as the first statement.
value = *(pointer + 0); // Take contents of pointer and add zero then dereference.
value = *(pointer + 1); // Deference the first location after the location in pointer.

Sometimes, especially in embedded systems, this is more readable than using the array notation. Example: status = *(UART + STATUS_REGISTER_OFFSET);

隐诗 2024-08-21 17:42:52

请注意这些表达式的等效性。首先,假设以下声明建立了正在使用的类型:

T* const base;
size_t const pos;

​​位于内存中的 T 类型的对象:

base + sizeof(T) * pos

现在,可以通过多种方式访问

base[pos]                            // Interpret base as an array.
*(base + pos)                        // Pointer arithmetic strides by sizeof(T)
*(T*)((char*)base + sizeof(T) * pos) // Compute stride manually

Note the equivalence of these expressions. First, assume the following declarations establish the types in play:

T* const base;
size_t const pos;

Now, an object of type T located in memory at position

base + sizeof(T) * pos

can be accessed in several ways:

base[pos]                            // Interpret base as an array.
*(base + pos)                        // Pointer arithmetic strides by sizeof(T)
*(T*)((char*)base + sizeof(T) * pos) // Compute stride manually
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文