Another pointer to array inside struct

Discussion in 'C Programming' started by drhowarddrfine, Dec 15, 2007.

  1. 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];
    drhowarddrfine, Dec 15, 2007
    #1
    1. Advertising

  2. drhowarddrfine

    Ben Pfaff Guest

    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,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;}}}
    Ben Pfaff, Dec 15, 2007
    #2
    1. Advertising

  3. On Dec 14, 7:42 pm, Ben Pfaff <> wrote:
    > 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,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.
    drhowarddrfine, Dec 15, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. beetle
    Replies:
    2
    Views:
    904
    beetle
    Jan 25, 2005
  2. Zero
    Replies:
    16
    Views:
    652
    Barry Schwarz
    Nov 19, 2005
  3. Mark McIntyre

    C beginner: struct inside another struct...

    Mark McIntyre, Apr 21, 2006, in forum: C Programming
    Replies:
    5
    Views:
    667
    Yourko
    Apr 23, 2006
  4. Tuan  Bui
    Replies:
    14
    Views:
    471
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    466
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page