为什么我可以分配结构但不能比较它们
尽管我是一名长期的 C 程序员,但我最近才知道可以直接将结构变量相互赋值,而不是使用 memcpy:
struct MyStruct a,b;
...
a = b; /* implicit memcpy */
虽然这对于 C 来说感觉有点“高级”,但它绝对有用。但为什么我不能进行平等和不平等比较:
if (a == b) ...
if (a != b) ...
标准有什么充分的理由排除这一点吗?或者这与——原本非常优雅的——标准不一致?
我不明白为什么我可以替换我的 memcpy 来进行干净的分配,但我必须保留那些丑陋的 memcmp 。
Even though I am a long time C programmer, I only recently learned that one can directly assign structure variables to one another instead of using memcpy:
struct MyStruct a,b;
...
a = b; /* implicit memcpy */
Though this feels a bit "high-level" for C, it is definitely useful. But why can't I do equality and inequality comparison:
if (a == b) ...
if (a != b) ...
Is there any good reason for the standard to exclude this? Or is this an inconsistency in the - otherwise very elegant - standard?
I don't see why I can replace my memcpy's for clean assignments, but I have to keep those ugly memcmp's in place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 comp.lang.c 常见问题解答:
Per the comp.lang.c FAQ: