C语言中如何判断数组中某个元素是否为空?
在 C 中如何检查数组中的元素是否为空?
if(array[i] == NULL)
似乎不起作用。
How can I check to see if an element in an array is empty in C?
if(array[i] == NULL)
Doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
空是什么意思?
当执行 C 程序时,未显式初始化的变量会获得不可预测的值。
您需要将所有数组单元格设置为 NULL(或设置为 0,或设置为程序逻辑中代表空的任何值),然后您可以按照您所做的方式检查它:
What do you mean with empty?
When a C program is executed, variables that you don't explicitly initialize have got unpredictable values.
You need to set all of your array cells to NULL (or to 0, or to whatever value represents emptyness in your program logic) and then you can check it in the way you did:
问题解答:
您发布的是正确的代码。
详细说明:
如果它“似乎不起作用”,也许问题不在于代码中的这个位置。如果您想发布一个更完整的示例来说明您所拥有的内容、代码的预期行为和实际行为,我们也许可以为您提供帮助。
Question answer:
What you posted is the correct code.
Elaboration:
If it "doesn't seem to work", perhaps the problem does not lie at this location in your code. If you would post a more complete example of what you have, the code's expected behavior and actual behavior, we may be able to help you.
假设
array
确实是一个指针数组,所提供的单行代码确实应该验证索引i
处的元素是否为 NULL。但请注意,如果您的数组未正确初始化(即:为每个单元格提供初始值),则该数组很可能包含垃圾,并且您的条件很可能最终为假。
Assuming that
array
is indeed an array of pointers, the single line of code provided should indeed verify that element at indexi
is NULL.Note however that if you array is not properly initialized (ie: provide an initial value to each cell), the array most probably contains garbage and your condition will most probably end up being false.
起初我在想,
“他们需要使用指针算术,所以
对象不会被自动取消引用
“[]”运算符。
然后我意识到,不...C 中的数组没有
空槽。
我的结论是,提问者是:
使用结构数组。
像使用数组一样使用它
指向结构的指针。
peoro的解决方案非常好。但我建议稍微修改一下。
如果您想以惰性/简单的方式执行此操作,请在结构中添加“.exists”属性。
简单并不是坏事,机器中的零件越多,可能出错的地方就越多。
下面的代码演示了两件事:
伪造稀疏数组,使用带有 .exists 标志修改的 peoro 解决方案。
使用双指针的实际稀疏数组。
当我在做的时候。如果您想从命令行运行,请将文件命名为:
“NAE.C99”,然后创建一个名为“NAE.SH”的bash文件并将其放入其中。
双击脚本运行它,或者使用它所在的“./NAE.SH”
git bash 终端。
顺便说一下,这是C99代码。不过,我尝试编写它以避免任何 C99 特定功能。
At first I was thinking,
"They need to use pointer arithmetic so the
object doesn't get auto de-referenced by the
"[ ]" operator.
Then I realized, no... Arrays in C don't have
null slots.
I conclude, the asker is:
Using an array of structs.
Using it as if it were an array of
pointers to structs.
peoro's solution is pretty good. But I would recommend modifying it a bit.
Add a ".exists" property to your struct if you want to do it the lazy/simple way.
Simple is not a bad thing, the more parts in a machine the more things that can go wrong.
Code below demonstrates two things:
Faking a sparse array using peoro's solution with .exists flag modification.
An actual sparse array using double pointers.
While I am at it. If you want to run from the command line, name the file:
"NAE.C99", then create a bash file called "NAE.SH" and put this into it.
Double click the script to run it, or use "./NAE.SH" where it resides in your
git bash terminal.
This is C99 code by the way. I try to write it avoiding any C99 specific features though.