我最近在看nginx的源代码。 其中包括以下代码:
ngx_max_module=0;
for(I=0; ngx_modules[i]; I ) {
ngx _ modules [ I ]-index=ngx _ max _ module;
}
ngx_modules[]数组的定义如下:
ngx_module_t *ngx_modules[]={
ngx_core_module,
ngx_errlog_module,
ngx_conf_module,
ngx_events_module,
ngx_event_core_module,
ngx_epoll_module,
ngx_http_module,
//省略
空值
}
在for循环中,用什么来判断何时停止遍历? for循环语句表示为:
for (式1; 式2; 式3 )
语句
语法上,for线性文的三个构成要素都是公式。 最常见的是公式1
公式3是赋值表达式或函数调用,公式2是关系表达式。
因为在c语言中,关系表达式的值非NULL和1表示真,NULL和0表示假。 因此,由于ngx_modules[]数组中的最后一个元素为NULL,因此在遍历到最后一个元素时,关系表达式的值为假,循环结束。
除非另有说明,否则本博客文章均为原创,转载请以链接形式注明正文地址
本文地址: http://blog.cnwyhx.com/p=215