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. 12 9836
"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.
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
> 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.
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.
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.
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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/',
|
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 -----------
|
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;
|
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 {...
|
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;
}
| |
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.
|
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
|
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;
|
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
{
|
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...
|
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,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |