我剛剛學習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