将数据复制到结构成员数组 [英] copying data to a structure member array
问题描述
大家好,
我正在尝试将数据复制到一个
结构成员的数组中。我现在正在做的是:
char array [8] = {0,1,2,3,4,5,6,7};
memcpy(structure.array,array,8);
在单个语句中有更好的方法吗?
我能想到的一种方法是使用
memcpy(structure.array," \ x0 \ x1 \ x2 \ x3 \ x4 \ x5 \ x6 \x7",8);
但是,如果我不想使用十六进制数字怎么办?例如,如果
我想将77,18,28,23,88,253,43,36小数复制到
structure.array [8],我会不喜欢先将所有这些
数转换为十六进制。如何在不使用另一个
变量的情况下执行此操作。
提前感谢您的帮助。
" anonymous" < CA ****** @ yahoo.com>在消息中写道
news:11 ********************* @ f14g2000cwb.googlegro ups.com ...大家好,
我正在尝试将数据复制到一个
结构成员的数组。我现在正在做的是:
char array [8] = {0,1,2,3,4,5,6,7};
memcpy(structure.array,数组,8);
在单个语句中有更好的方法吗?
它有什么问题?
我能想到的一种方法是使用memcpy(结构。 array,\x0 \ x1 \x2 \ x3 \ x4 \ x5 \ x6 \ x7",8);
这是另一种可能性,但我更喜欢第一个(更多
可读)。
然而,什么如果我不想使用十六进制数字。例如,如果我想将77,18,28,23,88,253,43,36小数复制到
structure.array [8],我不想先转换所有这些
数字到十六进制。如何在不使用其他
变量的情况下执行此操作。
使用额外变量(如您提供的第一个选项)有什么问题?它不会花费你更多的记忆,而且它会更清晰。
你使用的行数是*不是一个标准质量。
蒲公英写道:" anonymous" < CA ****** @ yahoo.com>在消息中写道
新闻:11 ********************* @ f14g2000cwb.googlegro ups.com ...你好伙计,
我正在尝试将数据复制到一个
结构成员的数组。我现在正在做的是:
char array [8] = {0,1,2,3,4,5,6,7};
memcpy(structure.array,数组,8);
有没有更好的方法在一个语句中这样做?
它有什么问题?
我能想到的一种方法是使用memcpy(structure.array," \ x0 \ x1 \ x2 \ x3 \ x4 \ x5 \ x6 \ x7" ;,8);
这是另一种可能性,但我更喜欢第一种(更可读)。
但是,如果我不想使用十六进制数字怎么办?例如,如果我想将77,18,28,23,88,253,43,36小数复制到
structure.array [8],我不想先转换所有这些
数字到十六进制。如何在不使用其他
变量的情况下执行此操作。
使用额外变量(如第一个选项
给了?它不会花费你更多的记忆,它会更清晰。
你使用的行数不是*质量的标准。
可能是质量。但我只是确保我不会错过更好的
方式。
谢谢
>我正在尝试将数据复制到一个数组,该数组是结构的成员。我现在正在做的是:
char array [8] = {0,1,2,3,4,5,6,7};
memcpy(structure.array,数组,8);
有没有更好的方法在一个语句中这样做?
不是真的。
你可以用一个语句初始化整个结构,
但这可能不是你想要的。
什么都没有不好关于你写的。如果你想要一种更类似于脚本语言的
语言,那么只需使用Java或Python即可。
。 ;-)
最后想一想,你应该写:
memcpy(structure.array,array,sizeof 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.
"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.
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
> I am in a fix trying to copy data to an array which is member of astructure. 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.
这篇关于将数据复制到结构成员数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!