// Online C compiler to run C program online
#include <stdio.h>
#include <stdlib.h>
char* word(char w[]) {
char *temp = w;
return temp;
}
char** tokens() {
char *one = word("One");
char *two = word("two");
char *three = word("three");
char *four = word("four");
char *five = word("five");
char *six = word("six");
char *seven = word("seven");
char *eight = word("eight");
char *nine = word("nine");
char *ten = word("ten");
char *eleven = word("ten");
char *twelve = word("ten");
char *thirteen = word("ten");
char *words[13];
words[0] = one;
words[1] = two;
words[2] = three;
words[3] = four;
words[4] = five;
words[5] = six;
words[6] = seven;
words[7] = eight;
words[8] = nine;
words[9] = ten;
words[10] = eleven;
words[11] = twelve;
words[12] = thirteen;
char **pp = words;
return pp;
}
int main() {
// Write C code here
char **pp = (char **)malloc(13 * sizeof(char));
pp = tokens();
for (int i = 0; i < 12; i++) {
printf("%s\n", *pp);
*pp++;
}
// for (; *pp; *pp++) {
// printf("%s\n", *pp);
// }
return 0;
}
I want the code to print every word contained in the tokens() method. However, in the for loop in main(), it only prints: one, two, three, four, null, and then gets a segmentation fault. If I use the for loop that is commented, it only prints: one, two, three, four. I'm new to C, so pointers are still pretty confusing. I'd appreciate any help. Thanks.