linux c之遍历字符串数组

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011068702/article/details/87995556

1 问题

比如我们要遍历字符串数组,我们的思路一般是先求字符串数组的长度,然后再用for循环便利,其实没必要这样,我们直接在

字符串数组后面加上个NULL就行再去遍历

2 代码实现

#include <stdio.h>

int main()
{
	static const char *data[] = {"chen", "yu", "hello", "word", NULL};
	for (int i = 0; data[i]; ++i)
	{
		printf("data is %s\n", data[i]);
	}
	return 0;	
}

3 运行结果

data is chen
data is yu
data is hello
data is word

 

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/87995556
今日推荐