如何找到关于C中的动态二维字符数组的行数? [英] How to find number of rows in dynamic 2D char array in C?

查看:111
本文介绍了如何找到关于C中的动态二维字符数组的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入功能,如:

I have an input function like :

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

其中字符数组s的随机提供作为例如:{sdadd,dsdsd,dsffsf,ffsffsf}

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

下面输出应为4上面的例子。

Here output should be 4 for above example.

推荐答案

传递的字符串数组需要更多的信息.​​..

Passing string arrays requires more information...

这是类似于你会发现在输入说呼叫INT主(INT ARGC,CHAR *的argv [])。唯一的例外是,当的main()被调用时,它读取命令行和提供统计资料,ARGC,为的确定的的参数数量在的argv [] 。这就是为什么的char * argv的[] 参数可以传递。

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.

您有相同的要求。这是传递一个参数,如的char * S [] ,也就是说,还必须以某种方式提供一个值,告诉 FUNC()多少字符串。 C具有无法知道该字符串变量的数量没有被告知的方式。这是因为数组引用(字符* S [])衰变为一个字符指针(字符* S;)指向该数组的,即没有数组大小信息

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.

所以,回答你的问题是: 与给出的信息 FUNC() 不能确定取值

一个重要的区别:结果
大小的 CAN 的决定字符数组,如

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 

在范围,在这种情况下,简单的说就是字符* S [] ...与全球知名度的定义,或在的功能计算元素的个数。如果作为参数传递,字符* S [] ;,如上所定义,将被简单地视为字符* s,而无法确定的参数的数目。

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.

检索一个字符串数组串计数其他选项包括:结果
1)修改您的输入数组,这样的东西最后一个字符串中是独一无二的,如%或\\ 0。这将允许您测试与标准的C字符串函数的字符串。

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)包含在提供串号码的功能参数。 (例如,类似于主(...)的printf(...)

这篇关于如何找到关于C中的动态二维字符数组的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆