473,508 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy struct to buffer

tom
how to copy struct to buffer?

char * buffer;
buffer=(char *)malloc(5*20);
Data_O tt;
strcpy(tt.Name1, "John ");
strcpy(tt.Name2, "Brown ");
memcpy(buffer + 0,(const void*)tt, sizeof (tt)); // dasn't work
strcpy(tt.Name1, "Tom ");
strcpy(tt.Name2, "Backer ");
memcpy(buffer + 5,(const void*)tt, sizeof (tt)); // dasn't work
....

struct Data_O
{
char Name1[10+1];
char Name2[10+1];
};
t.
Sep 20 '06 #1
2 9661
tom wrote:
how to copy struct to buffer?

char * buffer;
buffer=(char *)malloc(5*20);
Data_O tt;
strcpy(tt.Name1, "John ");
strcpy(tt.Name2, "Brown ");
Try this:

memcpy(buffer, &tt, sizeof tt);
memcpy(buffer + 0,(const void*)tt, sizeof (tt)); // dasn't work
strcpy(tt.Name1, "Tom ");
strcpy(tt.Name2, "Backer ");
memcpy(buffer + 5,(const void*)tt, sizeof (tt)); // dasn't work
memcpy(buffer + sizeof tt, &tt, sizeof tt);

Krishanu
Sep 20 '06 #2
char * buffer;
buffer=(char *)malloc(5*20);
Data_O tt;
strcpy(tt.Name1, "John ");
strcpy(tt.Name2, "Brown ");
memcpy(buffer + 0,(const void*)tt, sizeof (tt)); // dasn't work
tt is not a pointer, you must be use &tt.
strcpy(tt.Name1, "Tom ");
strcpy(tt.Name2, "Backer ");
memcpy(buffer + 5,(const void*)tt, sizeof (tt)); // dasn't work
Why not buffer + sizeof(tt) ?

Or:

char* buffer = new char[5 * sizeof(Data_O) ];
Data_O* tt = (Data_O*) buffer;
strcpy(tt->Name1, "John..");
strcpy(tt->Name2, "Brown");
tt = (Data_O*) (buffer + sizeof(Data_O));
strcpy(tt->Name1, "Tom");
strcpy(tt->Name2, "Backer");
....
delete[] buffer;

Sep 20 '06 #3

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

Similar topics

5
17568
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...
15
38056
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
3
2973
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
11
4135
by: akarui.tomodachi | last post by:
I have two structure definitions as below: typedef struct _sourceString { char firstSourceString; char secondSourceString; char thirdSourceString; }sourceString;
6
10120
by: Jim Showalter | last post by:
I'm trying to write code that gets fixed-length strings from the user and then stores them in a linked list. Here's the definition of my list node: struct node {char str; struct node* next; };...
8
40447
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
2
11859
by: samba | last post by:
hi, struct msg { struct header ht; char buf; }*mt; struct tlv { uint32_t tag;
4
2787
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
5
1498
by: Chris Saunders | last post by:
Here is the C declaration of a struct from Windows: typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { // // Used to return the required buffer size if return code is...
0
7118
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...
0
7323
Oralloy
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,...
0
7493
tracyyun
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...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
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...

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.