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

自古美人都是妖i 提交于 2020-01-01 16:53:09

问题


I have an input function like :

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

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

Here output should be 4 for above example.


回答1:


Passing string arrays requires more information...

This is similar to the input you would find in say a call to int main(int argc, char *argv[]). The exception is that when main() is called, it reads the command line and provides count information, in argc, to determine the number of arguments in argv[]. That is why the char *argv[] argument can be passed.

You have the same requirement. That is when passing an argument such as char *s[], i.e., you must also somehow provide a value telling func() how many strings. C has no way of knowing the number of strings in that variable without being told. This is because an array reference ( char *s[]; ) decays into a pointer to char ( char *s; ) pointing to the first element of that array, i.e., no array size information.

So, the answer to your question is:, with the information given, func() cannot determine the number of strings in s.

An important distinction:
The size CAN be determined for character arrays, such as

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

But only when s is declared in the same scope of the function attempting to determine the number of elements. If that condition is met, then use:

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 

In scope, in this context, simply means that char *s[]... be defined with global visibility, or within the function calculating number of elements. If passed as an argument, char *s[];, as defined above, will be simply seen as char *s, and the number of arguments cannot be determined.

Other options for retrieving count of strings in a string array include:
1) Modify your input array so that something in the last string is unique, like "%", or "\0". This will allow you to test strings with standard C string functions.

2) Include an argument in the function providing number of strings. (eg, similar to main(...) or printf(...))




回答2:


Like with all array parameters in C there is no way to do that, because arrays decay to a pointer to their first element (for a discussion cf. the C FAQ, http://c-faq.com/aryptr/) when passed as function parameters.

As always in this case you have two possibilities.

  1. Use an end marker. That works always if the element type of the array can hold a special value which does not occur "naturally" for that type. For chars that's '\0', for pointers like in your array that's NULL, the null pointer. For example argv[], the second argument to main(), is terminated by a null pointer. That makes iterating easy:

    #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;
    }
    
  2. Pass a length information with the array. That's, interestingly, the information in argc which the C runtime also passes to main().

Beyond that there is no (or at least no portable) way to know how many elements an array has which is passed as a parameter to a function in C.




回答3:


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

This statement will do. The variable size will be assigned the number of strings present in the *s[].



来源:https://stackoverflow.com/questions/19481251/how-to-find-number-of-rows-in-dynamic-2d-char-array-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!