I wanna have characters 'ab' for each element in a table, but in console I have this:
tab[0] = argvab
tab[1] = argvabab
tab[2] = argvababab
tab[3] = argvabababab
This is a part of my code:
char **tab = (char**)malloc((n) * sizeof(char*));
for (int i = 0; i < n; i++) {
tab[i] = argv[1];
strcat(tab[i], "ab");
printf("tab[%d] = %s\n", i, tab[i]);
}
tab[i] = argv[1];
strcat(tab[i], "ab");
You're not allowed to append things to the argv
strings. They're fixed size blocks with no extra space. Appending invokes undefined behavior.
Also even if you could you'd be appending to argv[1]
each time rather than argv[i]
.
Solution: (a) Change argv[1]
to argv[i]
, and (b) copy each argument before you append to it.