How to write the variables of a structure from an array?

Discussion in 'C Programming' started by gladhuman@hotmail.com, Apr 1, 2006.

  1. Guest

    Hi,

    Let's assume that I have the following structure and the following
    array:

    struc My_struct
    {
    char var_one;
    char var_two;
    char var_three;
    };

    struct My_struct testing;
    char My_array[3]={43, 13, 32};

    Is there a fast way of initializing the variables of "testing" with the
    values of the My_array?
    I would like the following

    testing.var_one = 43;
    testing.var_two = 13;
    testing.var_three = 32;

    Basically I am missing a function like fread which reads data from a
    file. Instead I would like to read it from an array. Having only three
    variables may not be an issue, but if the structure has 100 variables
    it is a tedious work to assign the values of each array-cell to each
    variable in the structure...

    I appreciate your help.


    Regards,
    GH.
     
    , Apr 1, 2006
    #1
    1. Advertising

  2. Guest

    Sorry folks,

    I found the answer. memcpy solves the problem.

    memcpy(testing, my_array, 3);

    Regards,
    GH.
     
    , Apr 1, 2006
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote:
    > Sorry folks,


    Please provide context even when replying to yourself. There is no
    guarantee that others will ever see the message that you are replying
    to. See http://cfaj.freeshell.org/google/ for how to reply properly
    using Google. Also see the link in my sig for more about this group.
    Since by change I have the message you were replying to I've reinstated
    it below:

    > wrote:
    >> Hi,
    >>
    >> Let's assume that I have the following structure and the following
    >> array:
    >>
    >> struc My_struct
    >> {
    >> char var_one;
    >> char var_two;
    >> char var_three;
    >> };
    >>
    >> struct My_struct testing;
    >> char My_array[3]={43, 13, 32};
    >>
    >> Is there a fast way of initializing the variables of "testing" with the
    >> values of the My_array?
    >> I would like the following
    >>
    >> testing.var_one = 43;
    >> testing.var_two = 13;
    >> testing.var_three = 32;
    >>
    >> Basically I am missing a function like fread which reads data from a
    >> file. Instead I would like to read it from an array. Having only three
    >> variables may not be an issue, but if the structure has 100 variables
    >> it is a tedious work to assign the values of each array-cell to each
    >> variable in the structure...
    >>
    >> I appreciate your help.


    > I found the answer. memcpy solves the problem.
    >
    > memcpy(testing, my_array, 3);


    No you haven't, you only think you have. The compiler is allowed to
    insert padding between field in a structure which would break the
    memcpy. So whilst it might work for you now it might not if you change
    compiler or change options on your existing compiler. I believe the only
    correct way to do it is one element at a time.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Apr 1, 2006
    #3
  4. writes:
    > Let's assume that I have the following structure and the following
    > array:
    >
    > struc My_struct
    > {
    > char var_one;
    > char var_two;
    > char var_three;
    > };
    >
    > struct My_struct testing;
    > char My_array[3]={43, 13, 32};
    >
    > Is there a fast way of initializing the variables of "testing" with the
    > values of the My_array?
    > I would like the following
    >
    > testing.var_one = 43;
    > testing.var_two = 13;
    > testing.var_three = 32;
    >
    > Basically I am missing a function like fread which reads data from a
    > file. Instead I would like to read it from an array. Having only three
    > variables may not be an issue, but if the structure has 100 variables
    > it is a tedious work to assign the values of each array-cell to each
    > variable in the structure...


    And in a followup, the original poster writes:
    > I found the answer. memcpy solves the problem.
    >
    > memcpy(testing, my_array, 3);


    The problem with this is that there's no guarantee that the elements
    of the structure are allocated contiguously. The compiler is free to
    insert padding between members and/or after the last member. Usually
    it will do so only as necessary to meet alignment requirements, but it
    can legally insert padding based on the phase of the moon.

    The only guaranteed portable way to do what you're trying to do is to
    assign each member:

    testing.var_one = my_array[0];
    testing.var_two = my_array[1];
    testing.var_three = my_array[2];

    The best approach is probably to decide in the first place whether you
    want to use an array or a structure, and use one or the other
    consistently.

    --
    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, Apr 1, 2006
    #4
  5. CBFalconer Guest

    wrote:
    >

    .... snip ...
    >
    > Basically I am missing a function like fread which reads data from
    > a file. Instead I would like to read it from an array. Having only
    > three variables may not be an issue, but if the structure has 100
    > variables it is a tedious work to assign the values of each array-
    > cell to each variable in the structure...
    >
    > I appreciate your help.


    I used the following to generate an arbitrary stream in the
    maxcount program I published here a few days ago. Some easy
    modifications would allow it to work with any array of values.

    size_t maxcount;
    #define initnext(count) maxcount = count

    /* get the next integer from the input stream */
    int next(void)
    {
    int value;

    if (!maxcount) return EOF;
    else maxcount--;
    do {
    value = (rand() % 65) - 32; /* symettric about 0 */
    } while (EOF == value);
    return value;
    } /* next */

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Apr 1, 2006
    #5
    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. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    795
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,303
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    644
  4. Replies:
    9
    Views:
    25,547
    Lal Bahadur Singh
    Nov 11, 2011
  5. Replies:
    9
    Views:
    1,015
Loading...

Share This Page