Web blog of yydzero
姚延栋(Yandong Yao)的博客
归档
« 十一月 2009
星期日星期一星期二星期三星期四星期五星期六
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
今天
Click me to subscribe
Search In My Blog

链接
 

今日点击: 50

Locations of visitors to this page
« C语言中可变长参数的处理 | Main | test »
星期二 七月 04, 2006
C tips from daily usage

确定数组的长度

通常我们需要在一个循环中遍历数组的所有元素,因此需要知道数组的长度。硬编码数组长度显然是不可取的,因为如果改变数组元素的个数的话,需要改动多处代码。使用下面的方式可以很好的避免这个问题:

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]);
}

[转载]NULL 0 \0以及nul的区别


信息来源: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"

 


匿名参数的使用

有时候我们需要使用匿名参数以匹配指定的接口,譬如g_list_foreach()函数需要他的函数参数有两个参数,而如果我们把g_free传递给他时会报告错误,这样我们就可以使用一个函数封装g_free。例如

o_free (gpoint p, gpoint /* data */)

Posted at 10:17下午 七月 04, 2006 by Yaodong Zero Yao in C&C++  |  评论[0]

评论:

发表一条评论:
  • HTML语法: 禁用