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 1

utchar(a[i&15]);break;}}}