C 中赋值的逻辑值
while (curr_data[1] != (unsigned int)NULL &&
((curr_ptr = (void*)curr_data[1]) || 1))
两部分问题。
从逻辑上讲,(curr_ptr = (void*)curr_data[1])
的计算结果是什么。 正确
?
另外,我知道它相当黑客,但是 while
语句合法的 C 语言吗?我必须经过巨大的扭曲才能将作业放在代码中的其他地方,所以如果我能把它留在那里,我会非常高兴,但如果它是如此令人震惊,以至于让每个人的眼球都燃烧起来,我会改变它。
while (curr_data[1] != (unsigned int)NULL &&
((curr_ptr = (void*)curr_data[1]) || 1))
Two part question.
What will (curr_ptr = (void*)curr_data[1])
evaluate to, logically. TRUE
?
Also, I know its rather hack-ish, but is the while
statement legal C? I would have to go through great contortions to put the assignment elsewhere in the code, so I'd be really nice if I could leave it there, but if it's so egregious that it makes everyone's eyeballs burst into flames, I'll change it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(curr_ptr = (void*)curr_data[1])
将计算为 TRUE,除非它是空指针。假设 curr_data 是一个指针数组,您想要做的是在第二个指针不为空时运行循环,同时将其值赋给 curr_ptr ,我会这样做:
(curr_ptr = (void*)curr_data[1])
will evaluate to TRUE unless it is a null pointer.Assuming that
curr_data
is an array of pointers, and what you want to do is to run the loop while the second of these pointers is not null, while assigning its value tocurr_ptr
, I would do:回答您的问题:
curr_ptr
未设置为NULL
(即curr_data[1]
不为 0),它将评估为 true 。不管怎样,我假设你没有写这段代码,因为你正在争论是把它留在里面还是把它拿出来。所以我想让你找出这行代码是谁写的,并向他们介绍一个沉重的钝器。
(unsigned int)NULL
很荒谬。你为什么要这样做?这可能与仅编写0
相同(不确定标准是否保证)。curr_data[1]
被转换为指针(并且指针被转换为它),那么 curr_data[1] 中的数据是什么样的?如果它应该将指针作为整型类型保存,则应使用
中提供的类型intptr_t
或uintptr_t
为此目的(如果您的编译器不支持 C99,ptrdiff_t
可能是可接受的替代品)。这可能很麻烦,但请认真重新考虑重写这一行。它看起来像是 IOCCC 中的一个条目。
To answer your questions:
curr_ptr
isn't set toNULL
(i.e.curr_data[1]
isn't 0).Anyway, I'm assuming you didn't write this code, because you're debating about leaving it in vs. taking it out. So I want you to find out who wrote this line of code and introduce them to a heavy blunt object.
(unsigned int)NULL
is ridiculous. Why would you do this? This will probably be the same as just writing0
(not sure if that's guaranteed by the standard).curr_data[1]
if it's being cast to a pointer (and pointers are being cast to it)? If it's supposed to be holding a pointer as an integral type, you should use the typeintptr_t
oruintptr_t
provided in<stdint.h>
for that purpose (if you're compiler doesn't support C99ptrdiff_t
may be an acceptable substitute).|| 1
at the end seems to be redundant. Ifcurr_ptr = (void*)curr_data[1]
would have evaluated to false, we would have caught that in the first condition.It may be a pain in the ass, but seriously reconsider rewriting this line. It looks like an entry in the IOCCC.
赋值是 C 语言中的表达式,所以你所拥有的都是有效的。将
;
更改为{}
意味着完全相同的事情,并且更清晰,至少要进行更改。当您有更清晰的替代方案(通常是正确的)时,应避免在条件中进行赋值,但如果此位置最清晰,则使用它。赋值的结果是被赋值的对象。
a = value
将执行赋值操作,然后计算结果为a
。这用于执行诸如a = b = 0
之类的操作。为了进一步清理代码,不需要 void 强制转换,如果这是字符,请使用“\0”(空字符)而不是 NULL(应该仅与指针一起使用)。
Assignments are expressions in C, so what you have works. Changing the
;
to{}
means the exact same thing and is much clearer, do that change at the very least. Assignments in conditions should be avoided when you have a clearer alternative (which is usually true), but if this is clearest in this place, then use it.The result of an assignment is the assigned-to object.
a = value
will do the assignment and then evaluate toa
. This is used to do things likea = b = 0
.To further clean up the code, there's no need for the void cast, and if this is chars, use '\0' (the null character) instead of NULL (which is supposed to be used with pointers only).
你不必经历“巨大的扭曲”,这完全相当于
You wouldn't have to go through "great contortions", that is completely equivalent to