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

G

gladhuman

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.
 
G

gladhuman

Sorry folks,

I found the answer. memcpy solves the problem.

memcpy(testing, my_array, 3);

Regards,
GH.
 
F

Flash Gordon

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:
>> 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.
 
K

Keith Thompson

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 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.
 
C

CBFalconer

.... 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/>
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top