难以理解 C 指针语法
给定以下 C 定义:
#define SYNC_BYTE_1 0x5A
#define SYNC_BYTE_2 0xA5
和指针声明:
UINT8 *pCommandData;
pCommandData = GetCommandBufferPointer( LINGO_GENERAL, stringLength + 3 );
以下两行代码到底对指针做了什么?
*pCommandData++ = SYNC_BYTE_1;
*pCommandData++ = SYNC_BYTE_2;
我特别不明白在这种情况下 *
和 ++
的使用。如果指针的地址递增,是否应该将 *
替换为 &
?
Given the following C definitions:
#define SYNC_BYTE_1 0x5A
#define SYNC_BYTE_2 0xA5
and pointer declaration:
UINT8 *pCommandData;
pCommandData = GetCommandBufferPointer( LINGO_GENERAL, stringLength + 3 );
What exactly are the following two lines of code doing with the pointer?
*pCommandData++ = SYNC_BYTE_1;
*pCommandData++ = SYNC_BYTE_2;
I specifically don't understand the use of *
and the ++
in this instance. If the pointer's address is being incremented shouldnt the *
be replaced with a &
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
pCommandData
是指向某块内存的指针。第一行将该地址处的值设置为
0x5A
,然后将指针pCommandData
递增到下一个地址。下一行的工作原理类似:它将
pCommandData
指向的值设置为0xA5
,然后将指针递增到下一个地址。也许一张图片会有用。在任一行执行之前,
pCommandData
指向的附近的内存可能如下所示:After
*pCommandData++ = SYNC_BYTE_1;
:And after
*pCommandData++ = SYNC_BYTE_2 ;
:pCommandData
is a pointer to some piece of memory. The first linesets the value at that address to
0x5A
, and then increments the pointerpCommandData
to the next address. The next lineworks similarly: it sets the value that
pCommandData
points to, to0xA5
, and then increments the pointer to the next address.Perhaps a picture would be useful. Before either line executes, the memory in the neighborhood of wherever
pCommandData
points to might look like this:After
*pCommandData++ = SYNC_BYTE_1;
:And after
*pCommandData++ = SYNC_BYTE_2;
:这:
相当于:
或:
This:
is equivalent to:
or:
的构造:
简单来说就是将
pCommandData
指向的值设置为SYNC_BYTE_1
,然后递增指针以指向下一个位置。由于 pCommandData 是指向无符号 8 位整数的指针,这意味着它将递增以指向下一个(后续)8 位值。The construct of:
Simply means set the value pointed to by
pCommandData
toSYNC_BYTE_1
, then increment the pointer to point to the next location. SincepCommandData
is a pointer to an unsigned 8-bit integer, that means it will be incremented to point to the next (following) 8-bit value.指针本身递增,以指向下一个字节。
这与编写的情况完全一样:
您可以使用
&pCommandData
来获取指针本身的地址,但这不是这里发生的情况。使用*pCommandData
可以让您访问指针指向的对象(如果有:-)。The pointer itself is being incremented, to point to the next byte.
This works exactly like if it had been written:
You would use
&pCommandData
to get the address of the pointer itself, but that is not what happens here. Using*pCommandData
gives you access to the object the pointer points to (if any :-).的效果
是:
The effect of:
is:
pCommandData 指向的数据块设置为
0x5A
,然后指针设置为下一个字节,该字节本身设置为0xA5
。最后指针被设置到内存中的下一个字节。您可以将代码重写为如下所示:
The chunk of data pCommandData is pointing to is set to
0x5A
, then the pointer is set to the next byte which is itself set to0xA5
. In the end the pointer is set to the next byte in the memory.You can rewrite the code to something like this:
它将同步字节值放入 pCommandData,这就是您看到 *pCommandData 的原因,之后您将其递增以转到下一个字节。
It's putting sync byte values into pCommandData, that's why you see *pCommandData, afterwards you increment it to go to next byte.
表达式
读作
*(p++) = rval ;
它指示编译器将
p
的值保存为 p1。编辑注意:我永远不会写
*p++
因为它非常混乱,除非你清楚运算符优先级。我会通过显式编写*(p++)
来明确我的意图。The expression
is read as
*(p++) = rval ;
which instructs the compiler to
p
as p1.rval
to the dereferenced thingy.edited to note: I'd never write
*p++
as it's very confusing unless you're clear on operator precedence. I'd make my intent clear by explicitly writing*(p++)
.Postfix
++
的优先级高于一元*
,因此*p++
形式的表达式将被解析为*(p++)< /代码>; IOW,您正在取消引用
p++
的结果。这在您的代码上下文中意味着什么?
给定表达式
*pCommandData++ = SYNC_BYTE_1
,以下是对所发生情况的粗略解释:对表达式
pCommandData++
进行求值。表达式的结果是pCommandData
的当前值,它是一个指针值。作为副作用,pCommandData
的值会更新。没有指定此更新发生的确切时间,除了它必须在下一个序列点之前发生这一事实之外。使用
*
运算符取消引用表达式pCommandData++
的结果。这个较大表达式的结果是一个左值(引用内存中对象的表达式,以便我们可以读取或修改该对象)。我们将值 SYNC_BYTE_1 分配给在步骤 2 中计算的左值。
从语义上讲,它与编写相同:
Postfix
++
has a higher precedence than unary*
, so expressions of the form*p++
are parsed as*(p++)
; IOW, you're dereferencing the result ofp++
.What does that mean in the context of your code?
Given the expression
*pCommandData++ = SYNC_BYTE_1
, here's a rough explanation of what's happening:The expression
pCommandData++
is evaluated. The result of the expression is the current value ofpCommandData
, which is a pointer value. As a side effect, the value ofpCommandData
is updated. Exactly when this update occurs is not specified beyond the fact that it must occur before the next sequence point.The result of the expression
pCommandData++
is dereferenced using the*
operator. The result of this larger expression is an lvalue (an expression that refers to an object in memory such that we can read or modify that object).We assign the value SYNC_BYTE_1 to the lvalue that was computed in step 2.
Semantically, it's the same as writing: