473,783 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_thr ee = 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
4 1564
Sorry folks,

I found the answer. memcpy solves the problem.

memcpy(testing, my_array, 3);

Regards,
GH.

Apr 1 '06 #2
gl*******@hotma il.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*******@hotma il.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_thr ee = 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
gl*******@hotma il.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_thr ee = 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_thr ee = 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_Keit h) 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
gl*******@hotma il.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.c om, 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/googlegroupsrep ly/>
Apr 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
4893
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
19
8053
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What is the purpose of using a void pointer ? Instead of declaring a pointer variable "void *", can I declare it as "char *" and then later on typcast it to whatever type
40
3056
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what I mean, please look at this feedback my me: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3074c204-04e4-4383-9dd2-d266472a84ac If you think I am right, please vote for this feedback.
4
1556
by: jimmygoogle | last post by:
OK I want to create a menu from an XML file. I am parsing it with JS. Now it seems to be working fine until it comes time to build the menu. I am building arrays in JS and the arrays are inside a function. How can I make these arrays been seen by other functions? Or cant I? here is an abridged snippet: var xmlDoc; if (document.implementation &&
11
16230
by: Vmusic | last post by:
Hi, I am trying to write out an array of string variables to Notepad. I can't get SendKeys to accept the string variable only literal quoted strings. I DO NOT want the hassle of writing to a file I DO NOT want to write to a report I DO NOT want to write to a form My user requires I write this array of string variables out to Notepad,
18
2636
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
8
2935
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to call the function, EncodeAsnUser. And it's returning OK but when I display the decoded data in another part of my application it shows no data has been decoded, all fiedls are either null or blanks. For some reason, I am not able to step through...
1
2591
by: sunil | last post by:
Hi, Am developing one shared library for one application. In that .so am having one global array of structure ( two more different structure pointers in this struct). Once the application is launched, then I am allocating the memory on the heap for the internal structures. To print the log messages I am using the below mentioned global variables and time header functions.
6
7072
by: Per Juul Larsen | last post by:
Hi. My Textfile looks like this Variable 1 Variable 2 Variable 3 Variable 4 On opening the application it will read the textfile, and assign each line of variable to at textfield in VB and then close the file.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.