如果我声明了一个像extern char **seasons
这样的外部变量。有关如何编写循环来迭代这个数组的任何提示?假设NULL值是停止条件?
答案 0 :(得分:3)
假设你的数组看起来像这样:
const char *data[] = {"This", "is", "an", "array", "of", "strings", NULL};
您可以像这样迭代:
const char** seasons = data;
int i;
for(i = 0; seasons[i] != NULL; ++i) {
printf("%s ", seasons[i]);
}
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
extern char **seasons;
int main(){
int i = 0;
while(seasons[i] != NULL){
printf(seasons[i]);
i++;
}
return 0;
}
答案 2 :(得分:0)
如果arr
是您的数组并且您希望停止在i
arr
包含NULL
的索引for
,则标准for (i = 0; arr[i]; i++)
dostuff(arr[i]);
循环将如下所示:
{{1}}
答案 3 :(得分:0)
以下是3种变体:
extern char **seasons;
size_t i;
for(i = 0; seasons[i]; i++) {
do_something(seasons[i]);
}
或
extern char **seasons;
char **i;
for(i = seasons; *i ; i++) {
do_something(*i);
}
或
extern char **seasons;
char **i = seasons;
while(*i) {
do_something(*i);
i++;
}