By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,502 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,502 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sorry folks,

I found the answer. memcpy solves the problem.

memcpy(testing, my_array, 3);

Regards,
GH.

Apr 1 '06 #2

P: n/a
gl*******@hotmail.com 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:
gl*******@hotmail.com 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
Apr 1 '06 #3

P: n/a
gl*******@hotmail.com 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) ks***@mib.org <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.
Apr 1 '06 #4

P: n/a
gl*******@hotmail.com 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/>
Apr 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.