简体   繁体   English

在C中如何获取char字符串指针数组中的项目数

[英]In C how to get number of items in array of pointers to char strings

I want to implement the following function to print the contents of several char strings which are referenced through a pointer array. 我想实现以下函数来打印通过指针数组引用的几个char字符串的内容。 How can I determine how many pointers there are without passing the total as an argument of the function? 如何在不传递总数作为函数参数的情况下确定有多少指针?

If it was an array of type int then it would be possible to use the sizeof() function but given that each item of my array is a pointer to a char string and each string could be of different length I don't think I can use this approach. 如果它是一个int类型的数组,那么就可以使用sizeof()函数,但是假设我的数组中的每个项都是一个指向char字符串的指针,并且每个字符串可能有不同的长度,我认为我不能使用这种方法。

void printCharArray(char *arr[]){

    int length = sizeof(arr) / sizeof(char); /* this does not give correct 
                                          number of items in the pointer array */

    for (int i=1;i<=length; i++) {
        printf("Array item: [%s]",arr[i]);
    }

}

There is no built-in way to do this as C does not track the number of elements in an array for you like this. 没有内置的方法来执行此操作,因为C不会像这样跟踪数组中的元素数量。

You have a couple of options: 你有几个选择:

  1. Pass the number of items in the array. 传递数组中的项目数。
  2. Set the last item to NULL so the code knows when it's reached the end. 将最后一项设置为NULL,以便代码知道它何时到达结尾。 (This is kind of how C strings are handled.) (这就是如何处理C字符串。)
  3. Otherwise modify your data structures to track the number of items in the array. 否则,请修改数据结构以跟踪数组中的项目数。

You cannot pass arrays as function arguments in C. Array names decay to pointers to their first element when used as a function argument, and you must separately specify the number of available array elements. 您不能将数组作为函数参数传递给C.当用作函数参数时,数组名称会衰减为指向其第一个元素的指针,您必须单独指定可用数组元素的数量。

(In the context of a function parameter type, T[] and T* and T[12] are identical, so your function paramter might as well be char ** arr .) (在函数参数类型的上下文中, T[]T*T[12]是相同的,因此您的函数参数也可能是char ** arr 。)

Like so: 像这样:

void printCharArray(char ** arr, size_t len) { /* ... */ }

int main()
{
    char * arr[10] = { "Hello", "World", /* ... */ };

    printCharArray(arr, sizeof(arr)/sizeof(*arr));
}

(Alternatively you can supply a "first" and a "one-past-the-end" pointer, which might be a bit more natural .) (或者你可以提供一个“第一个”和一个“一个接一个”的指针,这可能更自然一些 。)

If you set the element after the last valid element to null then you can do: 如果将最后一个有效元素之后的元素设置为null,则可以执行以下操作:

void printCharArray(char *arr[]){

    for (int i=0; arr[i] != NULL; i++) {
        printf("Array item: [%s]",arr[i]);
    }

}
void printCharArray(char *arr[]) {
    int length = sizeof(arr) / sizeof(char*); /* this does not give correct 
                                                 number of items in the pointer array */
    for (int i=0;i<=length; i++) {
        printf("Array item: [%s]",arr[i]);
    }
}

int main()
{
    char * arr[10] = { "Hello", "World", /* ... */ };
    printCharArray(arr); 
}

first I declared y as a global var. 首先我将y声明为全局var。 htmTags is a *char[]. htmTags是一个* char []。 then in main() 然后在main()

y = 0;
while( htmTags[y] ) {
        y++;
}

I did not add null at the end of array. 我没有在数组末尾添加null。 provides actual count of array elements 提供数组元素的实际数量

or this works too for (y=0;htmTags[y];y++) ; 或者这也适用于(y = 0; htmTags [y]; y ++);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM