Pointer to array inside a struct

D

drhowarddrfine

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

user923005

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

drhowarddrfine

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

pete

drhowarddrfine said:
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 */
 
P

pete

drhowarddrfine said:
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 */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top