【发布时间】:2020-11-01 16:46:30
【问题描述】:
如何遍历 char ** 数组?以及如何获取每个位置的值(获取key1、key2、key3)? 我有这个:
// Online C compiler to run C program online
#include <stdio.h>
#include <string.h>
int main() {
char **tree_keys = {"key1","key2","key3"};
printf("Key = %s\n", tree_keys);
int size = 0;
int i =0;
while(tree_keys[i] != '\0'){
size++;
i++;
}
printf("%s", size);
return 0;
}```
it returns segmentation fault
【问题讨论】:
-
这能回答你的问题吗? What does int argc, char *argv[] mean?
-
问题三:首先初始化无效;那么
tree_keys不是字符串,不能这样使用(就像你对printf所做的那样);而且它不会自动终止。不要在这里使用指向指针的指针,而是使用 array 指针:char const *tree_keys[] = { "key1", "key2", "key3", NULL };