I trying to create a function to return a char *tab[]
to a fill a char *tab1[]
#include <stdio.h>
#include <string.h>
char * fill(){
char *tab[2];
tab[0] = "text 1";
tab[1] = "text 2";
return tab;
}
int main(){
char *tab1[2];
tab1 = fill();
return 0;
}
I tried to use strcpy, strncpy, return a char **, malloc. I only can copy an index per time, like tab1[0] = tab[0], but I need to return the complete array in one time.
I am using a recursive function to fill the *tab[] and to do this I need to concatenate some strings and var:
void fill(int n, char *x, char *y, char *z, char *tab[]){
int i;
char text[40];
if(n == 1){
strcpy(text, "text 1 ");
strcat(text, x);
strcat(text, " text 2 ");
strcat(text, y);
tab[0] = text;
} else if(tab[n-1] == ""){
strcpy(text, "text 1 ");
strcat(text, x);
strcat(text, " text 2 ");
strcat(text, z);
strcat(text, "\ntext 1 ");
strcat(text, z);
strcat(text, " text 2 ");
strcat(text, y);
tab[n-1] = text;
if(n-1 > 1){
fill(n-1, x, z, y, tab);
}
}
}
And in the finish I need to return the tab[] to fill another tab in the main:
int main(){
int n = 2;
char *tab1[n];
fill(n, "a", "b", "c", tab1);
return 0;
}