Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to array inside a struct

Reply
Thread Tools

Pointer to array inside a struct

 
 
drhowarddrfine
Guest
Posts: n/a
 
      12-14-2007
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.
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      12-14-2007
On Dec 14, 2:55 pm, drhowarddrfine <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
 
 
 
drhowarddrfine
Guest
Posts: n/a
 
      12-15-2007
On Dec 14, 5:16 pm, user923005 <(E-Mail Removed)> wrote:
> On Dec 14, 2:55 pm, drhowarddrfine <(E-Mail Removed)> 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-15-2007
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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-15-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM



Advertisments