简体   繁体   English

指向char数组的指针数组

[英]array of pointers to a char array

gcc 4.4.4 c89 gcc 4.4.4 c89

However, I am having a problem trying to display all the animals. 但是,我在尝试展示所有动物时遇到了问题。

I have the following code. 我有以下代码。

I am trying display all the animals in the array. 我正在尝试显示数组中的所有动物。 So I have 3 array of pointers to char*. 所以我有3个指向char *的指针数组。 Then an array of pointers to these data sets. 然后是一个指向这些数据集的指针数组。

I have tried to control the inner loop for checking for a -1 and a NULL for the outer. 我试图控制内部循环以检查-1和外部的NULL。

void initialize_char_array()
{
    char *data_set1[] = {"dog", "cat", "bee", NULL};
    char *data_set2[] = {"rabbit", "ant", "snake", "rat", NULL};
    char *data_set3[] = {"cow", "lizard", "beaver", "bat", "hedgehog", NULL};

    char *ptr_char[] = {*data_set1, *data_set2, *data_set3, NULL};

    display_char_array(ptr_char);
}

void display_char_array(char **ptr_char)
{
    size_t inner = 0, outer = 0;

    for(outer = 0; ptr_char[outer] != NULL; outer++) {
        for(inner = 0; *ptr_char[inner] != -1; inner++) {
            printf("data [ %s ]\n", ptr_char[outer][inner]);
        }
    }
}

Many thanks for any suggestions, 非常感谢您的任何建议,

*data_set1 is the same as data_set1[0] . *data_set1data_set1[0]相同。 Here's a fixed version of what you trying to do. 这是您尝试做的固定版本。 IMHO it's matter of taste which are you using: index-variable or pointer-iterators in the loop, apparently compiler will generate the very same machine code. 恕我直言,这是您使用的问题:循环中的索引变量或指针迭代器,显然编译器将生成完全相同的机器代码。

// type of ptr_char changed
void display_char_array(char **ptr_char[])
{
    size_t inner = 0, outer = 0;

    for(outer = 0; ptr_char[outer] != NULL; outer++) {
        // check for NULL in inner loop!
        for(inner = 0; ptr_char[outer][inner] != NULL; inner++) {
            printf("data [ %s ]\n", ptr_char[outer][inner]);
        }
    }
}
void initialize_char_array()
{
    char *data_set1[] = {"dog", "cat", "bee", NULL};
    char *data_set2[] = {"rabbit", "ant", "snake", "rat", NULL};
    char *data_set3[] = {"cow", "lizard", "beaver", "bat", "hedgehog", NULL};

    // fixed
    char **ptr_char[] = {data_set1, data_set2, data_set3, NULL};

    display_char_array(ptr_char);
}

Given the way your initialize_char_array function initializes the ptr_char array, you will never be able to display all the animals. 如果您使用initialize_char_array函数初始化ptr_char数组的方式,您将永远无法显示所有动物。 You will only be able to display the first of each of your three lists. 您将只能显示三个列表中的每个列表的第一个。 If you want to be able to access all of your animals, you should first define ptr_char as an array of pointers to char pointers: char **ptr_char[] . 如果您希望能够访问所有动物,则应首先将ptr_char定义为指向char指针的指针数组: char **ptr_char[]

Then the display function should take a parameter of this type char *** as argument. 然后,显示函数应将类型为char ***的参数作为参数。 Yes, that's 3 levels of indirection. 是的,这是3个间接级别。 Then, don't use size_t variables to loop in your arrays, use a char ** one, and a char * . 然后,不要使用size_t变量在数组中循环,使用char **char *

It might help to go over the types of each array. 遍历每个数组的类型可能会有所帮助。 Remember that in most circumstances the type of an array expression is implicitly converted (decays) from N-element array of T to pointer to T , or T * 1 . 请记住,在大多数情况下,数组表达式的类型会从N-element array of T隐式转换(衰减) pointer to TT * 1的 pointer to T In the cases of data_set1, data_set2, and data_set3, T is char * , so the expression data_set1 is implicitly converted from 4-element array of char * to pointer to char * , or char ** . 在data_set1,data_set2和data_set3的情况下, Tchar * ,因此表达式data_set14-element array of char *隐式转换pointer to char *char ** pointer to char * The same is true for the other two arrays, as shown in the table below: 如下表所示,其他两个数组也是如此:

Array        Type            Decays to
-----        ----            ---------
data_set1    char *[4]       char **
data_set2    char *[5]       char **
data_set3    char *[6]       char **

If you're creating an array of these expressions (which is what you appear to be trying to do), then the array declaration needs to be 如果您要创建这些表达式的数组(这似乎是您想做的),则需要使用数组声明

char **ptr_char[] = {data_set1, data_set2, data_set3, NULL};

which gives us 这给了我们

Array        Type            Decays to
-----        ----            ---------
ptr_char     char **[4]      char ***

Thus, ptr_char is an array of pointers to pointers to char, or char **ptr_char[4] . 因此,ptr_char是一个指向char或char **ptr_char[4]的指针的指针数组。 When you pass ptr_char an an argument to the display function, it is again implicitly converted from type 4-element array of char ** to pointer to char ** , or char *** . 当将ptr_char作为参数传递给显示函数时,它将再次从4-element array of char **类型4-element array of char **隐式转换pointer to char **char *** pointer to char **


1. The exceptions to this rule are when the array expression is an operand of either the sizeof or & (address-of) operators, or if the expression is a string literal being used to initialize another array in a declaration. 1.此规则的例外情况是:数组表达式是sizeof& (地址)运算符的操作数,或者表达式是用于初始化声明中另一个数组的字符串文字。

I re-wrote your program after trying (unsuccessfully) to debug the version you wrote: 在尝试(未成功)调试您编写的版本后,我重新编写了您的程序:

#include <stdio.h>

void display_char_array(char ***ptr_char)
{
    for ( ; *ptr_char != NULL; ptr_char++ ) {
        char **data_set;
        for ( data_set = *ptr_char; *data_set != NULL; data_set++ ) {
            printf("data [ %s ]\n", *data_set);
        }
    }
}

void initialize_char_array()
{
    char *data_set1[] = {"dog", "cat", "bee", NULL};
    char *data_set2[] = {"rabbit", "ant", "snake", "rat", NULL};
    char *data_set3[] = {"cow", "lizard", "beaver", "bat", "hedgehog", NULL};

    char **ptr_char[] = { data_set1, data_set2, data_set3, NULL };

    display_char_array(ptr_char);
}

int main( void )
{
    initialize_char_array();

    return 0;
}

Your version would segfault and your use of pointers was very confusing! 您的版本将出现段错误,并且指针的使用非常令人困惑!

the mistake is was the initialization of ptr_char only with the first element from data_set?, see below: 错误是仅使用data_set中的第一个元素初始化ptr_char ?,请参见下文:

void initialize_char_array()
{
    char *data_set1[] = {"dog", "cat", "bee", NULL};
    char *data_set2[] = {"rabbit", "ant", "snake", "rat", NULL};
    char *data_set3[] = {"cow", "lizard", "beaver", "bat", "hedgehog", NULL};

    char **ptr_char[] = {data_set1, data_set2, data_set3, NULL};

    display_char_array(ptr_char);
}

void display_char_array(char ***p)
{
  while( *p )
  {
    while( **p )
    {
      puts(**p);
      ++*p;
    }
    ++p;
  }
}

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

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