468,512 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 9225
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roy Hills | last post: by
15 posts views Thread by Sourcerer | last post: by
3 posts views Thread by Leo Nunez | last post: by
11 posts views Thread by akarui.tomodachi | last post: by
6 posts views Thread by Jim Showalter | last post: by
8 posts views Thread by cman | last post: by
4 posts views Thread by OhKyu Yoon | last post: by
5 posts views Thread by Chris Saunders | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.