我刚刚学习C,有人可以解释为什么在打印数组的第一个元素后,下面的代码会产生分段错误?将字符串数组传递给函数C
工作代码的外观如何?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 8
void make(char ***array) {
*array = malloc(ELEMENTS * sizeof(char *));
(*array)[0] = "test0";
(*array)[1] = "test1";
(*array)[2] = "test2";
(*array)[3] = "test3";
(*array)[4] = "test4";
(*array)[5] = "test5";
(*array)[6] = "test6";
(*array)[7] = "test7";
(*array)[8] = "test8";
}
int main(int argc, char **argv)
{
char **array;
make(&array);
int i;
for (i = 0; i < ELEMENTS; ++i) {
printf("%s\n", array[i]);
free(array[i]);
}
free(array);
return 0;
}
其实,你需要阅读很多关于投注者,只是谷歌它,你会得到乐趣的信息... – Castiblanco 2013-04-05 13:35:16