\\ 0 QUOT;?;的char *的数组应该在'\\ 0'或QUOT结束 [英] Array of char* should end at '\0' or "\0"?

查看:96
本文介绍了\\ 0 QUOT;?;的char *的数组应该在'\\ 0'或QUOT结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有字符数组的指针

Lets say we have an array of char pointers

char* array[] = { "abc", "def" };

现在应该怎么放到底?

char* array[] = { "abc", "def", '\0' };

char* array[] = { "abc", "def", "\0" };

不过,这两部作品。我们只有把条件相应检查结束

Though, both works. We only have to put the condition to check the end accordingly

array[ index ] != '\0';

array[ index ] != "\0";

我的问题是哪一个是更好的办法?这是大多数程序员使用吗?

My question is which one is the better way? Which is used by most programmers?

修改

大多数的回答说,NULL比'\\ 0'和\\ 0更好。
但我始终认为,

Most answers say that NULL is better than '\0' and "\0". But I always thought that

NULL是一样的'\\ 0',这是同为0x0或0

NULL is same as '\0' which is same as 0x0 or 0

是不是错了?

推荐答案

我将与 NULL 结束。为什么?因为你不能做以下任一:

I would end it with NULL. Why? Because you can't do either of these:

array[index] == '\0'
array[index] == "\0"

第一个是比较一个的char * 字符,这是的你想要什么。你必须要做到这一点:

The first one is comparing a char * to a char, which is not what you want. You would have to do this:

array[index][0] == '\0'

第二个甚至不工作。你是一个比较的char * 的char * ,是的,但这种比较是没有意义的。它通过如果两个指针指向同一块内存。不能使用 == 来比较两个字符串,你必须使用的strcmp()函数,因为C有几个外串没有内置支持(我的意思是少数)语法细微。而以下内容:

The second one doesn't even work. You're comparing a char * to a char *, yes, but this comparison is meaningless. It passes if the two pointers point to the same piece of memory. You can't use == to compare two strings, you have to use the strcmp() function, because C has no built-in support for strings outside of a few (and I mean few) syntactic niceties. Whereas the following:

array[index] == NULL

工作得很好,并传达你的观点。

Works just fine and conveys your point.

这篇关于\\ 0 QUOT;?;的char *的数组应该在'\\ 0'或QUOT结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆