星期二 七月 04, 2006
星期二 七月 04, 2006
通常我们需要在一个循环中遍历数组的所有元素,因此需要知道数组的长度。硬编码数组长度显然是不可取的,因为如果改变数组元素的个数的话,需要改动多处代码。使用下面的方式可以很好的避免这个问题:
char * hero[][2] = {
{ "Guo Jing", "She Diao" },
{ "Ling Huchong", "Xiao ao Jiang Hu" },
{ "Yang Guo", "Shen Diao" },
{ "Hu Fei", "Xue Shan Fei Hu" },
};
for (i=0; i < sizeof (hero) / sizeof (*hero); i++)
{
printf ("%s come from %s\n", hero[i][0], hero[i][1]);
}
信息来源:cprogramming.com
NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are *not* interchangeable:
NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.
Use cc -E you can see what NULL means on solaris. Commonly NULL is defined by "#define NULL 0" or "#define NULL (void *)0"
0 can be used anywhere, it is the generic symbol for each type's zero value and the compiler will sort things out.
'\0' should be used only in a character context.
nul is not defined in C or C++, it shouldn't be used unless you define it yourself in a suitable manner, like:
#define nul '\0'
empty string "" is "\0"