下面的代码示例内部字符串数组,不打印test1
琴弦 - 包含在array
在main() function
修改功能
test5
但它的作品里面的make() function
我敢肯定的答案很简单,我将如何产生所需的结果?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 4
void make(char ***array) {
char p2[] = "test1 test2 test3 test4 test5";
char* token = strtok(p2, " ");
int i = 0;
while (token)
{
(*array)[i]= token;
token = strtok(NULL, " ");
i++;
}
printf("%s\n",(*array)[0]);
printf("%s\n",(*array)[1]);
printf("%s\n",(*array)[2]);
printf("%s\n",(*array)[3]);
printf("%s\n",(*array)[4]);
}
int main(int argc, char **argv) {
char **array;
make(&array);
int i;
for (i = 0; i < ELEMENTS; ++i) {
printf("%s\n", array[i]);
}
return 0;
}
此代码编译没有错误或警告,并且产生以下输出:
test1
test2
test3
test4
test5
yf�
���
我的预期的结果是有test1 - test5
打印两次,一旦make() function
内,一次在main() function
作为一个侧面说明,这只是我的第二篇文章,以stackoverflow,这是从我的第一个问题修改代码Passing a string array to a function in C
您仍在使您的阵列太小。 – 2013-04-05 15:26:59