检查字符数组是否为空的最佳方法
检查字符数组是否为空的最可靠方法是什么?
char text[50];
if(strlen(text) == 0) {}
或者
if(text[0] == '\0') {}
我需要做
memset(text, 0, sizeof(text));
if(strlen(text) == 0) {}
什么最有效的方法是什么?
Which is the most reliable way to check if a character array is empty?
char text[50];
if(strlen(text) == 0) {}
or
if(text[0] == '\0') {}
or do i need to do
memset(text, 0, sizeof(text));
if(strlen(text) == 0) {}
Whats the most efficient way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
给出这段代码:
接下来是关于这段代码的问题:
我闻到了混乱的味道。具体来说,在这种情况下:
...
text[]
的内容将未初始化且未定义。因此,strlen(text)
将返回未定义的结果。确保 C 字符串初始化为空字符串的最简单/最快的方法是将第一个字节设置为 0。
从那时起,strlen(text) 和非常快但- not-as-straightforward
(text[0] == 0)
测试都将检测空字符串。Given this code:
Followed by a question about this code:
I smell confusion. Specifically, in this case:
... the contents of
text[]
will be uninitialized and undefined. Thus,strlen(text)
will return an undefined result.The easiest/fastest way to ensure that a C string is initialized to the empty string is to simply set the first byte to 0.
From then, both
strlen(text)
and the very-fast-but-not-as-straightforward(text[0] == 0)
tests will both detect the empty string.取决于您的数组是否包含以空结尾的字符串。如果是这样,那么
应该足够了。
编辑:另一种方法是......
这可能效率较低,但清楚地表达了您的意图。
Depends on whether or not your array is holding a null-terminated string. If so, then
should be sufficient.
Edit: Another method would be...
which is potentially less efficient but clearly expresses your intent.
这将用于查找字符数组是否为空。它可能也是最快的。
如果
text
数组为空,这也会很快。如果它包含字符,则需要先计算其中的所有字符。This will work to find if a character array is empty. It probably is also the fastest.
This will also be fast if the
text
array is empty. If it contains characters it needs to count all the characters in it first.第二种方法几乎肯定是测试以 null 结尾的字符串是否为空的最快方法,因为它涉及一次读取和一次比较。在这种情况下这种方法当然没有什么问题,所以你不妨使用它。
第三种方法不检查字符数组是否为空;它确保字符数组为空。
The second method would almost certainly be the fastest way to test whether a null-terminated string is empty, since it involves one read and one comparison. There's certainly nothing wrong with this approach in this case, so you may as well use it.
The third method doesn't check whether a character array is empty; it ensures that a character array is empty.
第二个是最快的。 如果字符串确实为空,则使用
strlen
将关闭,但strlen
将始终迭代字符串的每个字符,因此如果它是 < em>不空,它会做比你需要的更多的工作。正如詹姆斯提到的,第三个选项在检查之前擦除字符串,因此检查总是会成功,但毫无意义。
The second one is fastest. Using
strlen
will be close if the string is indeed empty, butstrlen
will always iterate through every character of the string, so if it is not empty, it will do much more work than you need it to.As James mentioned, the third option wipes the string out before checking, so the check will always succeed but it will be meaningless.
如果您正在为闪存和/或 RAM 空间很小的微控制器进行编码,请使用此选项。使用
strlen
比检查第一个字节会浪费更多的闪存。上面的例子是最快的并且需要更少的计算。
Use this if you're coding for micro-controllers with little space on flash and/or RAM. You will waste a lot more flash using
strlen
than checking the first byte.The above example is the fastest and less computation is required.
上面取消引用指针“text”并检查它是否为零。或者:
The above dereferences the pointer 'text' and checks to see if it's zero. alternatively: