bit_cracker007
bit_cracker007

Reputation: 2569

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

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.

Upvotes: 3

Views: 10525

Answers (3)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

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.

Upvotes: 2

Sachin Chauhan
Sachin Chauhan

Reputation: 9

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[].

Upvotes: -2

ryyker
ryyker

Reputation: 23208

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(...))

Upvotes: 4

Related Questions