I am new to C language and I am exploring passing by reference, especially char pointer arrays. My first attempt was to split the lines read from a text file, which was stored in list[] in test(). check_length() counts the number of lines in the file.
However, I am still a bit confused with this concept. The second code worked if placed within the main logic but failed after running printf("%s",p); when I tried to split the data via split(). I hope that the pros here can help explain why my code didn't work. Thank you in advance.
Code that worked:
 void split(char **list,int size){
    int count =0;
    char *list2[size][5];
    for(count =1;count<6;count++){
        printf("\n%s",list[count]);
        int count2=0;
        for(char *p = strtok(list[count],"|"); (count2<5)&&p ; p=strtok(NULL,"|"))
        {
            list2[count][count2]=strdup(p);
            printf("\n%d %d %s",count,count2,list2[count][count2]);
            count2++;
        }
    }}
int main(){
int size=0;
size=check_length(size);
printf("%d\n",size);
size = size-4;
char *list[size];
test(list);
split(list,size);
return 0;}
Successful output snippet:
Contactless Thermommeter | CT          | Japan        | 1               | 6
0 0 Contactless Thermommeter
0 1  CT
0 2  Japan
0 3  1
0 4  6
2nd attempt:
void split(char **list, char **list2,char **list3, int size){
   
    int count2=0;
    for(int count = 1;count<size ;count++){
        int count3=0;
        if (count < 6)
        {
            printf("\n%d %s\n",count,list[count]);
            for(char *p = strtok(list[count],"|"); (count3<7)&&p ; p=strtok(NULL,"|"))
            {
                count2 = count-1;
                printf("%s",p);
                list2[count2][count3]=strdup(p);
                printf("\n%d %d %s",count2,count3,list2[count2][count3]);
                count3++;
            }
        }
        else if (count>6){
            printf("\n%d %s\n",count,list[count]);
            for(char *p = strtok(list[count],"|"); (count3<6)&&p ; p=strtok(NULL,"|"))
            {
                count2 = count -7;
                list3[count2][count3]=strdup(p);
                printf("\n%d %d %s",count2,count3,list3[count2][count3]);
                count3++;
            }
            count2++;
        }
    }
}
int main(){
int size=0;
size=check_length(size);
printf("%d\n",size);
size = size-4;
char *list[size];
char *list2[5][7];
char *list3[size-7][6];
test(list);
split(list,list2,list3,size);
return 0;}
Additional question: Is this the correct way to free strdup? I tried free(list2[count2][count3]) and the for loop only ran once, whereas free(p) didn't work.
void split(char **list, char* (*list2)[7], char*(*list3)[6], size_t size){
    int count =0;
    int count2=0;
    for(int count = 1;count<size ;count++){
        int count3=0;
        if (count < 6)
        {
            for(char *p = strtok(list[count],"|"); (count3<7)&&p ; p=strtok(NULL,"|"))
            {
                count2 = count-1;
                list2[count2][count3]=strdup(p);
                printf("\n1 %d %d %s",count2,count3,list2[count2][count3]);
                count3++;
                free(strdup(p));
            }
        }
