Pointer to array inside a struct

Discussion in 'C Programming' started by drhowarddrfine, Dec 14, 2007.

  1. What am I forgetting here. It's the last thing I have to fix after a
    day of coding.

    char *box[]={
    "red","blue","green"
    };
    struct item{
    char **container[3];
    };
    struct item *item[10];

    I want to set item[0]->container[0] to point to the strings of box[]
    but I just can't see why my many attempts don't work.
     
    drhowarddrfine, Dec 14, 2007
    #1
    1. Advertising

  2. drhowarddrfine

    user923005 Guest

    On Dec 14, 2:55 pm, drhowarddrfine <> wrote:
    > What am I forgetting here. It's the last thing I have to fix after a
    > day of coding.
    >
    > char *box[]={
    > "red","blue","green"};
    >
    > struct item{
    > char **container[3];};
    >
    > struct item *item[10];
    >
    > I want to set item[0]->container[0] to point to the strings of box[]
    > but I just can't see why my many attempts don't work.


    I guess you may be having a problem with levels of indirection. While
    container is an array of 3 pointer to pointer to char, box is an array
    of 3 pointer to char.

    I guess you really want something like this:
    #include <stdio.h>

    typedef char *Rgb[3];

    static const Rgb rgb = {"red", "blue", "green"};

    typedef struct RgbItem {
    Rgb rgb_;
    } rgbItem;


    int main(void)
    {
    rgbItem RgbList[10] = {0};

    RgbList[0].rgb_[0] = rgb[0];
    RgbList[0].rgb_[1] = rgb[1];
    RgbList[0].rgb_[2] = rgb[2];
    printf("%s %s %s\n", RgbList[0].rgb_[0], RgbList[0].rgb_[1],
    RgbList[0].rgb_[2]);
    return 0;
    }
     
    user923005, Dec 14, 2007
    #2
    1. Advertising

  3. On Dec 14, 5:16 pm, user923005 <> wrote:
    > On Dec 14, 2:55 pm, drhowarddrfine <> wrote:
    >
    > > What am I forgetting here. It's the last thing I have to fix after a
    > > day of coding.

    >
    > > char *box[]={
    > > "red","blue","green"};

    >
    > > struct item{
    > > char **container[3];};

    >
    > > struct item *item[10];

    >
    > > I want to set item[0]->container[0] to point to the strings of box[]
    > > but I just can't see why my many attempts don't work.

    >
    > I guess you may be having a problem with levels of indirection. While
    > container is an array of 3 pointer to pointer to char, box is an array
    > of 3 pointer to char.
    >
    > I guess you really want something like this:
    > #include <stdio.h>
    >
    > typedef char *Rgb[3];
    >
    > static const Rgb rgb = {"red", "blue", "green"};
    >
    > typedef struct RgbItem {
    > Rgb rgb_;
    >
    > } rgbItem;
    >
    > int main(void)
    > {
    > rgbItem RgbList[10] = {0};
    >
    > RgbList[0].rgb_[0] = rgb[0];
    > RgbList[0].rgb_[1] = rgb[1];
    > RgbList[0].rgb_[2] = rgb[2];
    > printf("%s %s %s\n", RgbList[0].rgb_[0], RgbList[0].rgb_[1],
    > RgbList[0].rgb_[2]);
    > return 0;
    >
    > }


    Thank you. Your example showed me I wasn't declaring my types
    correctly.
     
    drhowarddrfine, Dec 15, 2007
    #3
  4. drhowarddrfine

    pete Guest

    drhowarddrfine wrote:
    >
    > What am I forgetting here. It's the last thing I have to fix after a
    > day of coding.
    >
    > char *box[]={
    > "red","blue","green"
    > };
    > struct item{
    > char **container[3];
    > };
    > struct item *item[10];
    >
    > I want to set item[0]->container[0] to point to the strings of box[]
    > but I just can't see why my many attempts don't work.



    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    size_t index;
    char *box[] = {
    "red","blue","green"
    };
    struct item {
    char **container;
    };
    struct item item[10];

    item[0].container = box;
    for (index = 0; index != sizeof box / sizeof *box; ++index) {
    puts(item[0].container[index]);
    }
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Dec 15, 2007
    #4
  5. drhowarddrfine

    pete Guest

    drhowarddrfine wrote:
    >
    > What am I forgetting here. It's the last thing I have to fix after a
    > day of coding.
    >
    > char *box[]={
    > "red","blue","green"
    > };
    > struct item{
    > char **container[3];
    > };
    > struct item *item[10];
    >
    > I want to set item[0]->container[0] to point to the strings of box[]
    > but I just can't see why my many attempts don't work.


    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    size_t index;
    char *box[] = {
    "red","blue","green"
    };
    struct item {
    char **container[3];
    };
    struct item item[10];

    item[0].container[0] = box;
    for (index = 0; index != sizeof box / sizeof *box; ++index) {
    puts(item[0].container[0][index]);
    }
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Dec 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. beetle
    Replies:
    2
    Views:
    913
    beetle
    Jan 25, 2005
  2. Zero
    Replies:
    16
    Views:
    661
    Barry Schwarz
    Nov 19, 2005
  3. none
    Replies:
    8
    Views:
    404
  4. Tuan  Bui
    Replies:
    14
    Views:
    478
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    472
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page