我正在尝试复制数组指针并打印复制数组中的值,但是我遇到了分段错误。
void kosomo(char * test[10]) {
int j=0;
char * test2[10];
while (test[j] != NULL) {
test2[j]=&test[j]; // test[j] is the string, so with & it's the address
j++;
}
printf("%d ",*test2[0]); // trying to do * in order to print the string
}
int main() {
char * test[] = {"abc","def",NULL};
kosomo(test);
}
答案 0 :(得分:0)
请尝试此程序可以帮助您。我假设您要打印整个字符串数组。
#include <stdio.h>
void kosomo(char *test[10]) {
int j = 0;
char *test2[10];
while (test[j] != NULL) {
test2[j] = test[j];
j++;
}
test2[j] = NULL;
j=0;
while (test2[j] != NULL) {
printf("%s ", test2[j]);
j++;
}
}
int main() {
char *test[] = {"abc", "def", NULL};
kosomo(test);
return 0;
}
输出
abc def