Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Another pointer to array inside struct

Reply
Thread Tools

Another pointer to array inside struct

 
 
drhowarddrfine
Guest
Posts: n/a
 
      12-15-2007
Is there a better way to do this? At the end of this code, instead of
copying all the pointers from milk[0] to my_milk[0], and so on, I'm
sure there is a way to just make my_milk[]=milk[]?

typedef char *Milk[2];
typedef char *Bread[2];
Milk milk[]={
white, chocolate
};
Bread bread[]={
white,wheat
};
struct Groceries{
Milk my_milk;
Bread my_bread;
}
struct Groceries my_orders[10];

my_orders[0].my_milk[0]=milk[0];
my_orders[0].my_milk[1]=milk[1];
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-15-2007
drhowarddrfine <> writes:

> Is there a better way to do this? At the end of this code, instead of
> copying all the pointers from milk[0] to my_milk[0], and so on, I'm
> sure there is a way to just make my_milk[]=milk[]?
>
> typedef char *Milk[2];
> typedef char *Bread[2];
> Milk milk[]={
> white, chocolate
> };
> Bread bread[]={
> white,wheat
> };
> struct Groceries{
> Milk my_milk;
> Bread my_bread;
> }
> struct Groceries my_orders[10];
>
> my_orders[0].my_milk[0]=milk[0];
> my_orders[0].my_milk[1]=milk[1];


C doesn't allow arrays to be assigned directly. You can use
memcpy:
assert(sizeof my_orders[0].my_mink == sizeof milk);
memcpy(my_orders[0].my_milk, milk, sizeof milk);
or you can wrap your array in a structure, which can be assigned:
struct milk {
char *array[2];
};
struct milk milk = {{"white", "chocolate"}};
struct bread {
char *array[2];
};
struct bread bread = {{"white", "wheat"}};
struct groceries {
struct milk my_milk;
struct bread my_bread;
};
struct groceries my_orders[10];
my_orders[0].my_milk = milk;
(The above has not been tested or carefully proofread, and
furthermore I'm feeling tired at the end of a long week.)
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
 
drhowarddrfine
Guest
Posts: n/a
 
      12-15-2007
On Dec 14, 7:42 pm, Ben Pfaff <b...@cs.stanford.edu> wrote:
> drhowarddrfine <robbel...@gmail.com> writes:
> > Is there a better way to do this? At the end of this code, instead of
> > copying all the pointers from milk[0] to my_milk[0], and so on, I'm
> > sure there is a way to just make my_milk[]=milk[]?

>
> > typedef char *Milk[2];
> > typedef char *Bread[2];
> > Milk milk[]={
> > white, chocolate
> > };
> > Bread bread[]={
> > white,wheat
> > };
> > struct Groceries{
> > Milk my_milk;
> > Bread my_bread;
> > }
> > struct Groceries my_orders[10];

>
> > my_orders[0].my_milk[0]=milk[0];
> > my_orders[0].my_milk[1]=milk[1];

>
> C doesn't allow arrays to be assigned directly. You can use
> memcpy:
> assert(sizeof my_orders[0].my_mink == sizeof milk);
> memcpy(my_orders[0].my_milk, milk, sizeof milk);
> or you can wrap your array in a structure, which can be assigned:
> struct milk {
> char *array[2];
> };
> struct milk milk = {{"white", "chocolate"}};
> struct bread {
> char *array[2];
> };
> struct bread bread = {{"white", "wheat"}};
> struct groceries {
> struct milk my_milk;
> struct bread my_bread;
> };
> struct groceries my_orders[10];
> my_orders[0].my_milk = milk;
> (The above has not been tested or carefully proofread, and
> furthermore I'm feeling tired at the end of a long week.)
> --
> char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
> ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
> =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
> 2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}


Thank you. I thought the same thing but had some self doubt.
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57