How to find the number of rows in a dynamic 2D char array in C?

I have an input function, for example:

int func(char* s[])
{
    // return number of rows in the character array s
}

where the char array is provided randomly, for example: {"sdadd", "dsdsd", "dsffsf", "ffsffsf"}.

Here the output should be 4 for the example above.

+4
source share
3 answers

Passing string arrays requires more information ...

, , , int main(int argc, char *argv[]). , main() count, argc, argv[]. char *argv[] .

. , char *s[], .. - , func(), . C , . , (char * s [];) char (char * s;), , .

, :, func() s.

:
CAN ,

 char *s[]={"this","is","an","array"};  // the assignment of values in {...}
                                        //make this an array of strings. 

, s , . , :

int size = sizeof(s)/sizeof(s[0]);  //only works for char arrays, not char *  
                                    //and only when declared and defined in scope of the call 

, char * s []... , . , char * s [];, , char * s, .

:
1) , - , , "%" "\ 0". .

2) , . (, main(...) printf(...))

+4

C, , ( . C FAQ, http://c-faq.com/aryptr/) .

.

  • . , , "" . , '\ 0', , , NULL, . argv[], main() . :

    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        int i;
        for(i=0; argv[i] != NULL; i++)
        {
            printf("%s\n", argv[i]);
        }
        return 0;
    }
    
  • . , argc, C main().

, (, , ) , , C.

+2
int size = sizeof(s)/sizeof(s[0]);

. , *s[].

-1

All Articles