C语言 获取未知长度的字符串数组的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3783562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:30:37  来源:igfitidea点击:

Get length of string array of unknown length

cpointersarrays

提问by Mohit Deshpande

I have this function:

我有这个功能:

int setIncludes(char *includes[]);

I don't know how many values includeswill take. It may take includes[5], it may take includes[500]. So what function could I use to get the length of includes?

我不知道需要多少值includes。可能需要includes[5],可能需要includes[500]。那么我可以使用什么函数来获取 的长度includes

回答by paxdiablo

There is none. That's because arrays will decay to a pointer to the first element when passing to a function.

空无一人。那是因为数组在传递给函数时会衰减为指向第一个元素的指针。

You have to either pass the length yourself or use something in the array itself to indicate the size.

您必须自己传递长度或使用数组本身中的某些内容来指示大小。



First, the "pass the length" option. Call your function with something like:

首先,“传递长度”选项。使用以下内容调用您的函数:

int setIncludes (char *includes[], size_t count) {
    // Length is count.
}
:
char *arr[] = {"Hello,", "my", "name", "is", "Pax."};
setIncludes (arr, sizeof (arr) / sizeof (*arr));
setIncludes (arr, 2); // if you don't want to process them all.


A sentinel method uses a special value at the end to indicate no more elements (similar to the \0at the end of a C chararray to indicate a string) and would be something like this:

哨兵方法在末尾使用一个特殊值来表示不再有元素(类似于\0在 Cchar数组末尾表示字符串),并且将是这样的:

int setIncludes (char *includes[]) {
    size_t count = 0;
    while (includes[count] != NULL) count++;
    // Length is count.
}
:
char *arr[] = {"Hello,", "my", "name", "is", "Pax.", NULL};
setIncludes (arr);


Another method I've seen used (mostly for integral arrays) is to use the first item as a length (similar to Rexx stem variables):

我见过使用的另一种方法(主要用于整数数组)是使用第一项作为长度(类似于 Rexx 词干变量):

int setIncludes (int includes[]) {
    // Length is includes[0].
    // Only process includes[1] thru includes[includes[0]-1].
}
:
int arr[] = {4,11,22,33,44};
setIncludes (arr);

回答by sukru

You have two options:

您有两个选择:

  1. You can include a second parameter, similar to:

    int main(int argc, char**argv)

  2. ... or you can double-null terminate the list:

    char* items[] = { "one", "two", "three", NULL }

  1. 您可以包含第二个参数,类似于:

    int main(int argc, char**argv)

  2. ...或者你可以双空终止列表:

    char* items[] = { "one", "two", "three", NULL }

回答by JaredPar

There is no way to simply determine the size of an arbitrary array like this in C. It requires runtime information that is not provided in a standard way.

在 C 中无法像这样简单地确定任意数组的大小。它需要以标准方式未提供的运行时信息。

The best way to support this is to take in the length of the array in the function as another parameter.

支持这一点的最好方法是将函数中数组的长度作为另一个参数。

回答by Anindya Chatterjee

Though it is a very old thread, but as a matter of fact you can determine the length of an arbitrary string array in C using Glib. See the documentation below:

虽然它是一个非常古老的线程,但实际上您可以使用 Glib 确定 C 中任意字符串数组的长度。请参阅以下文档:

https://developer.gnome.org/glib/2.34/glib-String-Utility-Functions.html#g-strv-length

https://developer.gnome.org/glib/2.34/glib-String-Utility-Functions.html#g-strv-length

Provided, it must be null terminated array of string.

提供,它必须是空终止的字符串数组。

回答by AraK

You have to know the size either way. One way would be to pass the size as a second parameter. Another way is to agree with the caller the he/she should include a null pointer as the last element in the passed array of pointers.

无论哪种方式,您都必须知道尺寸。一种方法是将大小作为第二个参数传递。另一种方法是与调用者达成一致,他/她应该在传递的指针数组中包含一个空指针作为最后一个元素。

回答by Mareg

And what about strlen() function?

那么 strlen() 函数呢?

  char *text= "Hello Word";
  int n= strlen(text);
OR
  int n= (int)strlen(text);