copying data to a structure member array

A

anonymous

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.
 
D

dandelion

anonymous said:
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.
 
A

anonymous

dandelion said:
anonymous said:
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
 
G

Guillaume

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.
 
A

anonymous

Guillaume said:
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.
 
A

anonymous

Guillaume said:
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.
 
L

Lawrence Kirby

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
 
A

Alberto =?iso-8859-1?Q?Gim=E9nez?=

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?
 
J

Jens.Toerring

Alberto Giménez said:
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
 
C

Chris Torek

... 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.)
 
K

Keith Thompson

Chris Torek said:
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]);
 
B

Ben Pfaff

Keith Thompson said:
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.
 
K

Keith Thompson

Ben Pfaff said:
Keith Thompson said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top