Another pointer to array inside struct

D

drhowarddrfine

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];
 
B

Ben Pfaff

drhowarddrfine said:
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.)
 
D

drhowarddrfine

drhowarddrfine said:
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,0xa67f6aaa,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:putchar(a[i&15]);break;}}}

Thank you. I thought the same thing but had some self doubt.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top