C 中赋值的逻辑值

发布于 2024-08-12 17:05:57 字数 359 浏览 5 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

悲凉≈ 2024-08-19 17:05:57

(curr_ptr = (void*)curr_data[1]) 将计算为 TRUE,除非它是空指针。

假设 curr_data 是一个指针数组,您想要做的是在第二个指针不为空时运行循环,同时将其值赋给 curr_ptr ,我会这样做:

while ((curr_ptr = (void*)curr_data[1]) != NULL) { ... }

(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 to curr_ptr, I would do:

while ((curr_ptr = (void*)curr_data[1]) != NULL) { ... }
冷了相思 2024-08-19 17:05:57

回答您的问题:

  1. 如果 curr_ptr 未设置为 NULL(即 curr_data[1] 不为 0),它将评估为 true 。
  2. 我相信这是合法的,但是这行代码存在更大的问题。

不管怎样,我假设你没有写这段代码,因为你正在争论是把它留在里面还是把它拿出来。所以我想让你找出这行代码是谁写的,并向他们介绍一个沉重的钝器。

  1. (unsigned int)NULL 很荒谬。你为什么要这样做?这可能与仅编写 0 相同(不确定标准是否保证)。
  2. 如果 curr_data[1] 被转换为指针(并且指针被转换为它),那么 curr_data[1] 中的数据是什么样的?如果它应该将指针作为整型类型保存,则应使用 中提供的类型 intptr_tuintptr_t为此目的(如果您的编译器不支持 C99,ptrdiff_t 可能是可接受的替代品)。
  3. <代码>||最后的1似乎是多余的。如果 curr_ptr = (void*)curr_data[1] 的计算结果为 false,我们就会在第一个条件下捕获它。

这可能很麻烦,但请认真重新考虑重写这一行。它看起来像是 IOCCC 中的一个条目。

To answer your questions:

  1. It will evaluate to true if curr_ptr isn't set to NULL (i.e. curr_data[1] isn't 0).
  2. I believe it's legal, but there are bigger problems with this line of code.

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.

  1. (unsigned int)NULL is ridiculous. Why would you do this? This will probably be the same as just writing 0 (not sure if that's guaranteed by the standard).
  2. What kind of data is in 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 type intptr_t or uintptr_t provided in <stdint.h> for that purpose (if you're compiler doesn't support C99 ptrdiff_t may be an acceptable substitute).
  3. The || 1 at the end seems to be redundant. If curr_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.

一场春暖 2024-08-19 17:05:57

赋值是 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 to a. This is used to do things like a = 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).

迷荒 2024-08-19 17:05:57

你不必经历“巨大的扭曲”,这完全相当于

while (curr_data[1]) {
    curr_ptr = (void *)curr_data[1];

You wouldn't have to go through "great contortions", that is completely equivalent to

while (curr_data[1]) {
    curr_ptr = (void *)curr_data[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文