I want to iterate through **list_arg[]
.
I can't make this work, unless I try to iterate through it in another function, then I can do list_arg++
.
int main(int argc, char *argv[]) {
char *three[] = {"Ver", NULL}
char *two[] = {"john", NULL};
char *one[] = {"Majka", NULL};
char **list_arg[] = { one, two, three, NULL};
while (**list_arg != NULL) {
printf("%s", **list_arg);
list_arg++; //invalid increase
//(*list_arg)++; iterates through one[] only.
//(**list_arg)++; iterates through *one[], aka "Majka" only.
}
return 0;
}
When using arrays of pointers (and especially arrays of pointers to pointers), it is generally better to use the specific []
operator, rather than trying to work out which combination of dereferencing (*
) operators will get to (and manipulate) the required target.
The following code does what (I think) you want (I have added an extra name to one of the arrays, to show that the iteration does work):
#include <stdio.h>
int main()
{
char* three[] = { "Ver", NULL };
char* two[] = { "John", "Fred", NULL}; // Add extra one here for demo
char* one[] = { "Majka", NULL };
char** list_arg[] = {one, two, three, NULL};
for (size_t i = 0; list_arg[i] != NULL; ++i) {
char** inner = list_arg[i];
for (size_t j = 0; inner[j] != NULL; ++j) {
printf("%s ", inner[j]);
}
printf("\n"); // New line to separate inner arrays
}
return 0;
}