Home > Net >  How to loop array of pointers to characters
How to loop array of pointers to characters

Time:03-30

Is there any way to loop array of character pointers? I was able to get the number of items in **args but how to get number of its each element so we can print its characters. As we can see each element column dimension differs. So is there any way in C to loop this?

int main()
{
    
    char *argv_c1[3] = {"cat","f.txt",NULL};
    char *argv_c2[2]={"sort",NULL};
    char *argv_c3[2]={"uniq",NULL};
    char *argv_c4[3]={"grep","day",NULL};
    char *argv_c5[3]={"wc","-l",NULL};

    char **args[]={argv_c1, argv_c2, argv_c3, argv_c4, argv_c5};

    unsigned long int total = sizeof(args)/sizeof(args[0]);
    printf("ld\n",total);
    
    for(int i=0;i<total;i  )
    {
        //how to loop argv_c1, argv_c2 ... argv_c5 ?
    }


    return 0;
}

CodePudding user response:

This will loop over the elements of args[i]:

for(int j=0; args[i][j]!=NULL; j  )
  • Related