473,671 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copying data to a structure member array

Hi folks,

I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?

One way that I can think of is to use

memcpy(structur e.array, "\x0\x1\x2\x3\x 4\x5\x6\x7", 8);

However, what if I don't want to use hex numbers. For example, if
I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
structure.array[8], I would not like to first convert all these
numbers to hex. How do I do this without having to use another
variable.

Thanks in advance for any help.

Nov 14 '05 #1
12 9825

"anonymous" <ca******@yahoo .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi folks,

I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?
What's wrong with it?
One way that I can think of is to use

memcpy(structur e.array, "\x0\x1\x2\x3\x 4\x5\x6\x7", 8);
That's another possibility, but i liked the first one better (more
readable).
However, what if I don't want to use hex numbers. For example, if
I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
structure.array[8], I would not like to first convert all these
numbers to hex. How do I do this without having to use another
variable.


What's wrong with using an extra variable like the first option you gave? It
won't cost you any more memory and it's a lot clearer.

The amount of lines you use is *not* an criterion for quality.
Nov 14 '05 #2

dandelion wrote:
"anonymous" <ca******@yahoo .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi folks,

I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?
What's wrong with it?
One way that I can think of is to use

memcpy(structur e.array, "\x0\x1\x2\x3\x 4\x5\x6\x7", 8);


That's another possibility, but i liked the first one better (more
readable).
However, what if I don't want to use hex numbers. For example, if
I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
structure.array[8], I would not like to first convert all these
numbers to hex. How do I do this without having to use another
variable.


What's wrong with using an extra variable like the first option you

gave? It won't cost you any more memory and it's a lot clearer.

The amount of lines you use is *not* an criterion for quality.


May be the quality. But I was just making sure I do not miss a better
way of doing this.

Thanks

Nov 14 '05 #3
> I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?


Not really.
You can initialize the whole struct with a single statement though,
but that might not be what you're after.

There is nothing "not nice" about what you wrote. If you want a
language that more closely resembles a scripting language, just
use Java or Python. ;-)

Just a final thought, you should have written:

memcpy(structur e.array, array, sizeof array);

much better.
Nov 14 '05 #4

Guillaume wrote:
I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?
Not really.
You can initialize the whole struct with a single statement though,
but that might not be what you're after.

There is nothing "not nice" about what you wrote. If you want a
language that more closely resembles a scripting language, just
use Java or Python. ;-)

No, actually I know only little of Java and nothing of
Python yet.
Just a final thought, you should have written:

memcpy(structur e.array, array, sizeof array);

much better.

Oh, yes of course, I think that should be the way even when
I am just quoting the statement in this post.

Nov 14 '05 #5

Guillaume wrote:
I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?


Not really.
You can initialize the whole struct with a single statement though,
but that might not be what you're after.

There is nothing "not nice" about what you wrote. If you want a
language that more closely resembles a scripting language, just
use Java or Python. ;-)

Just a final thought, you should have written:

memcpy(structur e.array, array, sizeof array);

much better.


Just one more thought. When there is escaping hex and octal
numbers, I think there should have been some choice for decimal
numbers as well.

If I could do

memcpy(structur e.array, "\x0\x1\x2\x3\x 4\x5\x6\x7", 8);

there should have been a way of doing something like the
following:

memcpy(structur e.array, "\d0\d1\d2\d3\d 4\d5\d6\d7", 8);
Just my thought.

Nov 14 '05 #6
On Tue, 04 Jan 2005 06:24:46 -0800, anonymous wrote:
Hi folks,

I am in a fix trying to copy data to an array which is member of a
structure. What I am doing right now is:

char array[8] = {0,1,2,3,4,5,6, 7};


Consider

static const char array[8] = {0,1,2,3,4,5,6, 7};

It tells the reader and compiler that this is a set up once compile time
constant and both can make appropriate deductions from that.

Lawrence
Nov 14 '05 #7
El Tue, 4 Jan 2005 15:41:35 +0100, dandelion escribió:
char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?


What's wrong with it?


What about if structure has padding? Can the memset function add wrong
'offsets' to the real values?
--
Luis Alberto Giménez
JabberID: Si*******@amess age.de
GnuPG ID: 0x3BAABDE1
Nov 14 '05 #8
Alberto Giménez <al****@telelin e.es> wrote:
El Tue, 4 Jan 2005 15:41:35 +0100, dandelion escribió:
char array[8] = {0,1,2,3,4,5,6, 7};
memcpy(structur e.array, array, 8);

Is there a nicer way of doing this in a single statement?


What's wrong with it?


What about if structure has padding? Can the memset function add wrong
'offsets' to the real values?


No - if there's padding in the structure it's between the members,
i.e. in this case before and/or after the 'array' member, but not
between the elements of the array 'structure.arra y'.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #9
In article <11************ *********@f14g2 000cwb.googlegr oups.com>
anonymous <ca******@yahoo .com> wrote:
... One way that I can think of is to use

memcpy(structu re.array, "\x0\x1\x2\x3\x 4\x5\x6\x7", 8);

However, what if I don't want to use hex numbers. For example, if
I want to copy 77, 18, 28, 23, 88, 253, 43, 36 decimals to
structure.arra y[8], I would not like to first convert all these
numbers to hex. How do I do this without having to use another
variable.


Using an auxiliary variable is reasonably clean and entirely portable
and therefore probably the best solution (and as Lawrence Kirby
notes, you can make it "static const" as well). In C89 this is in
fact the *only* solution if the array has any type other than (some
variant of) char, because the only anonymous array builder is the
string literal, and it only produces char (and wchar_t, which I am
loosely including in "some variant of" here :-) ).

In C99, however, you can use the new anonymous array syntax to
create an array object whose address is then passed to memcpy:

struct S { int array[8]; } structure;
...
memcpy(structur e.array,
(const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
sizeof(int [8]));

(The array built in this fashion is an object, so it undergoes the
transform prescribed by The Rule. Making it "const" allows the
compiler to generate a single copy at compile time in all cases;
without the "const", the compiler has to be clever enough to figure
out on its own that a single copy suffices, and I suspect many will
not -- so you would get one static const copy, and a second
dynamically-created copy on each pass through the code.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

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

Similar topics

1
2792
by: Reed Law | last post by:
I have the exact same data in two arrays, but only the array created like so will work: $spaw_imglibs = array( array( 'value' => '/youth/pics/Member pics/', 'text' => 'Member pics', ), array( 'value' => '/youth/pics/Group pictures/',
21
3923
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
5
17628
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
6
4194
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
11
3179
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
10
4776
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
0
1823
by: Brian Black | last post by:
Hi, Sorry For the Cross Posting But, I just started programming .net and i have a question. I have a byte array of 14 bytes (CODE BELOW), i need to copy the data from the the byte array into the Public Structure H_I_FuelStruct
31
7735
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
2
7200
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
8476
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8393
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,...
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
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
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
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
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.