copying data to a structure member array

Discussion in 'C Programming' started by anonymous, Jan 4, 2005.

  1. anonymous

    anonymous Guest

    Hi folks,

    I am in a fix trying to copy data to an array which is member of a
    structure. What I am doing right now is:

    char array[8] = {0,1,2,3,4,5,6,7};
    memcpy(structure.array, array, 8);

    Is there a nicer way of doing this in a single statement?

    One way that I can think of is to use

    memcpy(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);

    However, what if I don't want to use hex numbers. For example, if
    I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
    structure.array[8], I would not like to first convert all these
    numbers to hex. How do I do this without having to use another
    variable.

    Thanks in advance for any help.
    anonymous, Jan 4, 2005
    #1
    1. Advertising

  2. anonymous

    dandelion Guest

    "anonymous" <> wrote in message
    news:...
    > Hi folks,
    >
    > I am in a fix trying to copy data to an array which is member of a
    > structure. What I am doing right now is:
    >
    > char array[8] = {0,1,2,3,4,5,6,7};
    > memcpy(structure.array, array, 8);
    >
    > Is there a nicer way of doing this in a single statement?


    What's wrong with it?

    > One way that I can think of is to use
    >
    > memcpy(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);


    That's another possibility, but i liked the first one better (more
    readable).

    > However, what if I don't want to use hex numbers. For example, if
    > I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
    > structure.array[8], I would not like to first convert all these
    > numbers to hex. How do I do this without having to use another
    > variable.


    What's wrong with using an extra variable like the first option you gave? It
    won't cost you any more memory and it's a lot clearer.

    The amount of lines you use is *not* an criterion for quality.
    dandelion, Jan 4, 2005
    #2
    1. Advertising

  3. anonymous

    anonymous Guest

    dandelion wrote:
    > "anonymous" <> wrote in message
    > news:...
    > > Hi folks,
    > >
    > > I am in a fix trying to copy data to an array which is member of a
    > > structure. What I am doing right now is:
    > >
    > > char array[8] = {0,1,2,3,4,5,6,7};
    > > memcpy(structure.array, array, 8);
    > >
    > > Is there a nicer way of doing this in a single statement?

    >
    > What's wrong with it?
    >
    > > One way that I can think of is to use
    > >
    > > memcpy(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);

    >
    > That's another possibility, but i liked the first one better (more
    > readable).
    >
    > > However, what if I don't want to use hex numbers. For example, if
    > > I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
    > > structure.array[8], I would not like to first convert all these
    > > numbers to hex. How do I do this without having to use another
    > > variable.

    >
    > What's wrong with using an extra variable like the first option you

    gave? It
    > won't cost you any more memory and it's a lot clearer.
    >
    > The amount of lines you use is *not* an criterion for quality.


    May be the quality. But I was just making sure I do not miss a better
    way of doing this.

    Thanks
    anonymous, Jan 4, 2005
    #3
  4. anonymous

    Guillaume Guest

    > I am in a fix trying to copy data to an array which is member of a
    > structure. What I am doing right now is:
    >
    > char array[8] = {0,1,2,3,4,5,6,7};
    > memcpy(structure.array, array, 8);
    >
    > Is there a nicer way of doing this in a single statement?


    Not really.
    You can initialize the whole struct with a single statement though,
    but that might not be what you're after.

    There is nothing "not nice" about what you wrote. If you want a
    language that more closely resembles a scripting language, just
    use Java or Python. ;-)

    Just a final thought, you should have written:

    memcpy(structure.array, array, sizeof array);

    much better.
    Guillaume, Jan 4, 2005
    #4
  5. anonymous

    anonymous Guest

    Guillaume wrote:
    > > I am in a fix trying to copy data to an array which is member of a
    > > structure. What I am doing right now is:
    > >
    > > char array[8] = {0,1,2,3,4,5,6,7};
    > > memcpy(structure.array, array, 8);
    > >
    > > Is there a nicer way of doing this in a single statement?

    >
    > Not really.
    > You can initialize the whole struct with a single statement though,
    > but that might not be what you're after.
    >
    > There is nothing "not nice" about what you wrote. If you want a
    > language that more closely resembles a scripting language, just
    > use Java or Python. ;-)

    No, actually I know only little of Java and nothing of
    Python yet.
    >
    > Just a final thought, you should have written:
    >
    > memcpy(structure.array, array, sizeof array);
    >
    > much better.

    Oh, yes of course, I think that should be the way even when
    I am just quoting the statement in this post.
    anonymous, Jan 4, 2005
    #5
  6. anonymous

    anonymous Guest

    Guillaume wrote:
    > > I am in a fix trying to copy data to an array which is member of a
    > > structure. What I am doing right now is:
    > >
    > > char array[8] = {0,1,2,3,4,5,6,7};
    > > memcpy(structure.array, array, 8);
    > >
    > > Is there a nicer way of doing this in a single statement?

    >
    > Not really.
    > You can initialize the whole struct with a single statement though,
    > but that might not be what you're after.
    >
    > There is nothing "not nice" about what you wrote. If you want a
    > language that more closely resembles a scripting language, just
    > use Java or Python. ;-)
    >
    > Just a final thought, you should have written:
    >
    > memcpy(structure.array, array, sizeof array);
    >
    > much better.


    Just one more thought. When there is escaping hex and octal
    numbers, I think there should have been some choice for decimal
    numbers as well.

    If I could do

    memcpy(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);

    there should have been a way of doing something like the
    following:

    memcpy(structure.array, "\d0\d1\d2\d3\d4\d5\d6\d7", 8);
    Just my thought.
    anonymous, Jan 4, 2005
    #6
  7. On Tue, 04 Jan 2005 06:24:46 -0800, anonymous wrote:

    > Hi folks,
    >
    > I am in a fix trying to copy data to an array which is member of a
    > structure. What I am doing right now is:
    >
    > char array[8] = {0,1,2,3,4,5,6,7};


    Consider

    static const char array[8] = {0,1,2,3,4,5,6,7};

    It tells the reader and compiler that this is a set up once compile time
    constant and both can make appropriate deductions from that.

    Lawrence
    Lawrence Kirby, Jan 4, 2005
    #7
  8. El Tue, 4 Jan 2005 15:41:35 +0100, dandelion escribió:
    >> char array[8] = {0,1,2,3,4,5,6,7};
    >> memcpy(structure.array, array, 8);
    >>
    >> Is there a nicer way of doing this in a single statement?

    >
    > What's wrong with it?


    What about if structure has padding? Can the memset function add wrong
    'offsets' to the real values?


    --
    Luis Alberto Giménez
    JabberID:
    GnuPG ID: 0x3BAABDE1
    Alberto =?iso-8859-1?Q?Gim=E9nez?=, Jan 6, 2005
    #8
  9. anonymous

    -berlin.de Guest

    Alberto Giménez <> wrote:
    > El Tue, 4 Jan 2005 15:41:35 +0100, dandelion escribió:
    >>> char array[8] = {0,1,2,3,4,5,6,7};
    >>> memcpy(structure.array, array, 8);
    >>>
    >>> Is there a nicer way of doing this in a single statement?

    >>
    >> What's wrong with it?

    >
    > What about if structure has padding? Can the memset function add wrong
    > 'offsets' to the real values?


    No - if there's padding in the structure it's between the members,
    i.e. in this case before and/or after the 'array' member, but not
    between the elements of the array 'structure.array'.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Jan 6, 2005
    #9
  10. anonymous

    Chris Torek Guest

    In article <>
    anonymous <> wrote:
    >... One way that I can think of is to use
    >
    >memcpy(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);
    >
    >However, what if I don't want to use hex numbers. For example, if
    >I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
    >structure.array[8], I would not like to first convert all these
    >numbers to hex. How do I do this without having to use another
    >variable.


    Using an auxiliary variable is reasonably clean and entirely portable
    and therefore probably the best solution (and as Lawrence Kirby
    notes, you can make it "static const" as well). In C89 this is in
    fact the *only* solution if the array has any type other than (some
    variant of) char, because the only anonymous array builder is the
    string literal, and it only produces char (and wchar_t, which I am
    loosely including in "some variant of" here :) ).

    In C99, however, you can use the new anonymous array syntax to
    create an array object whose address is then passed to memcpy:

    struct S { int array[8]; } structure;
    ...
    memcpy(structure.array,
    (const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
    sizeof(int [8]));

    (The array built in this fashion is an object, so it undergoes the
    transform prescribed by The Rule. Making it "const" allows the
    compiler to generate a single copy at compile time in all cases;
    without the "const", the compiler has to be clever enough to figure
    out on its own that a single copy suffices, and I suspect many will
    not -- so you would get one static const copy, and a second
    dynamically-created copy on each pass through the code.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Jan 11, 2005
    #10
  11. Chris Torek <> writes:
    [...]
    > In C99, however, you can use the new anonymous array syntax to
    > create an array object whose address is then passed to memcpy:
    >
    > struct S { int array[8]; } structure;
    > ...
    > memcpy(structure.array,
    > (const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
    > sizeof(int [8]));


    I think you have an extra set of parentheses there.

    memcpy(structure.array,
    const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
    sizeof(int [8]);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 11, 2005
    #11
  12. anonymous

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > I think you have an extra set of parentheses there.
    >
    > memcpy(structure.array,
    > const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
    > sizeof(int [8]);


    I don't see how your "corrected" code could possibly be right.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Jan 11, 2005
    #12
  13. Ben Pfaff <> writes:
    > Keith Thompson <> writes:
    >
    >> I think you have an extra set of parentheses there.
    >>
    >> memcpy(structure.array,
    >> const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
    >> sizeof(int [8]);

    >
    > I don't see how your "corrected" code could possibly be right.


    You're right (as was Chris Torek), and I was wrong. I misread his
    code and thought the second and third lines were enclosed in a set of
    parentheses. Sorry.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 11, 2005
    #13
    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. MiniDisc_2k2

    Re: array copying to another array

    MiniDisc_2k2, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    344
    Philip Lawatsch
    Jul 13, 2003
  2. John Harrison

    Re: array copying to another array

    John Harrison, Jul 13, 2003, in forum: C++
    Replies:
    2
    Views:
    389
    John Harrison
    Jul 15, 2003
  3. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    723
    Martin Ambuhl
    Oct 26, 2004
  4. Jason
    Replies:
    2
    Views:
    487
    Jonathan Mcdougall
    May 13, 2006
  5. A
    Replies:
    27
    Views:
    1,557
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page