I know there are other ways to solve this like with a calloc but the question here is why does it work while it shouldn't?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function (char **, char **);
int main () {
char *list[2], *list1[] = {"word0", "word1", "word2"};
function(list, list1);
return 0;
}
void function (char **list, char **list1) {
for(int i=0; i<2; i++) {
printf("Insert string\n");
scanf("%s", (list+i)); //adds 2 strings in list
}
for(int i=0; i<2; i++) {
printf("%s\n", (list+i)); //reads the strings that I just added but if I place * before (list+i) the code crashes
}
printf("%s\n", *(list1+1)); //reads the second string of list1 but if I don't place * before (list+1) it reads some weird chars
}
I was trying to better understand pointers and arrays of strings, I expected that it would not work the way it's written in the comments in the code. Like why is the "*" needed before (list1+1) and not before (list+i)