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

copying data to a structure member array

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a

"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

P: n/a

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

P: n/a
> 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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.