无法理解 C 源代码,它不能在 GCC 中编译,而是在 Visual C++ 中编译。
在 GCC 中我收到以下错误:
aes.c:在函数“copy_block”中:
aes.c:278: 错误:需要左值作为增量操作数
aes.c:278:错误:需要左值作为增量操作数
这是一段代码:
static void copy_block( void * d, void *s, uint_8t nn )
{
while( nn-- )
*((uint_8t*)d)++ = *((uint_8t*)s)++;
}
我试图将其更改为可编译版本,但不幸的是,对于我作为 Java 程序员来说,不清楚这里到底发生了什么。
也许有人知道我如何更改它在 GCC 中可编译的源代码,或者有人知道这里详细发生了什么。对我来说,左手值的取消引用似乎很奇怪,但不知怎的,它似乎在 Visual C++ 中完美工作。
这是一个小的遗留程序,我必须将其移植到 Linux 机器上。
提前感谢您的帮助。
In GCC I got the following error:
aes.c: In function ‘copy_block’:
aes.c:278: error: lvalue required as increment operand
aes.c:278: error: lvalue required as increment operand
This is the piece of code:
static void copy_block( void * d, void *s, uint_8t nn )
{
while( nn-- )
*((uint_8t*)d)++ = *((uint_8t*)s)++;
}
I tried to change it to a compileable version, but unfortunately for me as a Java programmer, it's not clear what is really happening here.
Maybe someone has an idea how I can change the source that it is compileable in GCC or someone has an idea what is happening here in detail. For me it seems wierd with the dereferencing of the left hand value, but somehow it seems to work perfectly in Visual C++.
This is a small legacy program, which I have to port to a Linux machine.
thank you for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
那里所做的事情并不好。
Try this:
What's being done there isn't good.
这应该可以做到:
它也更具可读性。
This should do it:
It's also much more readable.
这是一些看起来非常毛茸茸的代码。看起来 *(pointer)++ 子表达式的优先级规则存在问题。 (Visual C++ 接受它介于奇迹和错误之间)它似乎只是一个(可怕的)memcpy 重新实现,所以我建议您使用标准版本:
memcpy(d, s, nn);
我建议在 Visual C++ 中进行快速特征测试,以确保两个版本的代码实际上执行相同的操作(正如它们看起来的那样)。您可能在这里依赖于编译器中的一些奇怪的边缘情况。
This is some seriously hairy looking code. Looks like there's an issue with precedence rules on the *(pointer)++ subexpressions. (that Visual C++ accepts it is somewhere between miracle and bug) It seems to be just a (terrible) memcpy reimplementation, so I suggest you use the standard version:
memcpy(d, s, nn);
I suggest doing a quick characterisation test in Visual C++ to make sure the two versions of the code do in fact do the same thing (as they appear to). You might be relying on some quirky edge case in the compiler here.
问题是强制转换返回右值,但您试图对强制转换返回的指针执行自动增量。试试这个:
另外两个注意事项:
nn
可能应该比uint8_t
大很多,并且可能应该是size_t
。The problem is that casts return rvalues, but you're trying to do an autoincrement on the pointer returned by a cast. Try this:
Two other notes:
nn
should probably be a good deal larger thanuint8_t
and should probably besize_t
.memcpy
and is found (probably more efficiently) in your standard library (instring.h
I believe).您可能搞砸了括号位置。
您想要增加指针,而不是取消引用的值。
作为奖励提示,使用 memcpy ...更快!
You probably screwed up the parenthesis position.
You want to increment the pointer, not the dereferenced value.
As a bonus tip, use memcpy ... much faster !
它是 为什么这个转换的结果不是一个重复的左值?
基本上强制转换不是左值。但它可以与某些编译器一起使用。不仅是旧的 GCC,最近的 Tasking 也“正确”编译并且代码可以工作。
It's a duplicate of Why isn't the result of this cast an lvalue?
Basically casts are not l-value. It works with some compilers though. Not only old GCC, quite recent Tasking also compiled "properly" and the code worked.
应该是:
Should be: