The following code example, doesn't print the strings test1 - test5 contained in array in the main() function
however it works inside the make() function
I am certain the answer is simple, how would I produce the desired result?
#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;
}
This code compiles without error or warning and produces the following output:
test1
test2
test3
test4
test5
yf�
���
my expected result was to have test1 - test5 printed twice, once inside the make() function and once in the main() function
as a side note, this is only my second post to stackoverflow, this is modified code from my first question Passing a string array to a function in C