473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(structure.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(structure.array, "\x0\x1\x2\x3\x4\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 9790

"anonymous" <ca******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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(structure.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(structure.array, "\x0\x1\x2\x3\x4\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*********************@f14g2000cwb.googlegro ups.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(structure.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(structure.array, "\x0\x1\x2\x3\x4\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(structure.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(structure.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(structure.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(structure.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(structure.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(structure.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(structure.array, "\x0\x1\x2\x3\x4\x5\x6\x7", 8);

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

memcpy(structure.array, "\d0\d1\d2\d3\d4\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(structure.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*******@amessage.de
GnuPG ID: 0x3BAABDE1
Nov 14 '05 #8
Alberto Giménez <al****@teleline.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(structure.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.array'.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #9
In article <11*********************@f14g2000cwb.googlegroups. com>
anonymous <ca******@yahoo.com> wrote:
... One way that I can think of is to use

memcpy(structure.array, "\x0\x1\x2\x3\x4\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.


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(structure.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
Chris Torek <no****@torek.net> writes:
[...]
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(structure.array,
(const int [8]){77, 18, 28, 23, 88, 253, 43, 36},
sizeof(int [8]));


I think you have an extra set of parentheses there.

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

--
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.
Nov 14 '05 #11
Keith Thompson <ks***@mib.org> writes:
I think you have an extra set of parentheses there.

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


I don't see how your "corrected" code could possibly be right.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #12
Ben Pfaff <bl*@cs.stanford.edu> writes:
Keith Thompson <ks***@mib.org> writes:
I think you have an extra set of parentheses there.

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


I don't see how your "corrected" code could possibly be right.


You're right (as was Chris Torek), and I was wrong. I misread his
code and thought the second and third lines were enclosed in a set of
parentheses. Sorry.

--
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.
Nov 14 '05 #13

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

Similar topics

1
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', ),...
21
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...
5
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...
6
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...
11
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...
10
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...
0
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...
31
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.