linux c之遍历字符串数组
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
赞 (0)