Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中查找动态2D字符数组中的行数?_C_Multidimensional Array_Arrays - Fatal编程技术网

如何在C中查找动态2D字符数组中的行数?

如何在C中查找动态2D字符数组中的行数?,c,multidimensional-array,arrays,C,Multidimensional Array,Arrays,我有一个输入函数,如: int func(char* s[]) { // return number of rows in the character array s } 其中随机提供字符数组s,例如:{“sdadd”、“dsdsd”、“dsffsf”、“ffsfsfsf”} 以上示例的输出应为4。传递字符串数组需要更多信息… 这类似于您在调用intmain(intargc,char*argv[])中找到的输入。例外情况是,当调用main()时,它读取命令行并以argc提供计数信息,以

我有一个输入函数,如:

int func(char* s[])
{
    // return number of rows in the character array s
}
其中随机提供字符数组s,例如:{“sdadd”、“dsdsd”、“dsffsf”、“ffsfsfsf”}

以上示例的输出应为4。

传递字符串数组需要更多信息…

这类似于您在调用
intmain(intargc,char*argv[])
中找到的输入。例外情况是,当调用
main()
时,它读取命令行并以argc提供计数信息,以确定
argv[]
中的参数数量。这就是为什么可以传递
char*argv[]
参数的原因

你也有同样的要求。这是在传递诸如
char*s[]
之类的参数时,也就是说,您还必须以某种方式提供一个值,告诉
func()
有多少个字符串。C无法在不被告知的情况下知道该变量中的字符串数。这是因为数组引用(char*s[];)衰减为指向该数组第一个元素的char(char*s;)指针,即没有数组大小信息

因此,您的问题的答案是:,根据给出的信息,
func()
无法确定
s
中的字符串数

一个重要的区别
可以为字符数组确定大小,例如

 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”。这将允许您使用标准的C字符串函数测试字符串

2)在提供字符串数量的函数中包含一个参数。(例如,类似于
main(…)
printf(…)


这句话就行了。变量大小将被分配到
*s[]

中存在的字符串数量,就像C中的所有数组参数一样,这是无法做到的,因为数组作为函数参数传递时会衰减为指向其第一个元素的指针(有关讨论,请参阅C FAQ)

像往常一样,在这种情况下,你有两种可能

  • 使用结束标记。如果数组的元素类型可以包含一个特殊的值,而该值对于该类型来说并不是“自然”出现的,那么这种方法总是有效的。对于“\0”字符,对于数组中的空指针,则为空指针。例如,
    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;
    }
    
    #包括
    int main(int argc,char*argv[])
    {
    int i;
    对于(i=0;argv[i]!=NULL;i++)
    {
    printf(“%s\n”,argv[i]);
    }
    返回0;
    }
    
  • 将长度信息与数组一起传递。有趣的是,这是argc中的信息,C运行时也将其传递给
    main()


  • 除此之外,没有(或至少没有可移植的)方法来知道数组中有多少元素作为参数传递给C中的函数。

    通常(例如,在main的
    argv
    中)指针数组中的最后一个元素是空指针,因此遍历它相当于针对空进行测试:
    for(int i=0;s[i];i++){/*use s[i]*/}
    。初始化看起来像
    {“sdadd”、“dsdsd”、“dsffsf”、“ffsfsfsf”、NULL}
    如果我错了,则正确,但是如果内存是用malloc等动态分配的,则此检查将不起作用。
    #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;
    }