简体   繁体   中英

array of pointers to a char array

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*. 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.

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] . 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. 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[] .

Then the display function should take a parameter of this type char *** as argument. Yes, that's 3 levels of indirection. Then, don't use size_t variables to loop in your arrays, use a char ** one, and a 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 . 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 ** . 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] . 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 *** .


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.

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:

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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