Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to write the variables of a structure from an array?

Reply
Thread Tools

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

 
 
gladhuman@hotmail.com
Guest
Posts: n/a
 
      04-01-2006
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.

 
Reply With Quote
 
 
 
 
gladhuman@hotmail.com
Guest
Posts: n/a
 
      04-01-2006
Sorry folks,

I found the answer. memcpy solves the problem.

memcpy(testing, my_array, 3);

Regards,
GH.

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      04-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:

> (E-Mail Removed) 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-01-2006
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-01-2006
(E-Mail Removed) 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/>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments