memcpy() memcat()?

Started by
9 comments, last by Unsuspected 20 years, 11 months ago
quote:Original post by Way Walker
If your unsigned char isn''t a pointer, I can only think of two other things.

1) It''s just a single unsigned char, in which case you have one byte to work with, and nobody can help you

2) It''s an array, and you aren''t familiar with the way people talk about arrays in C and C++


Actually, it is
3) It was the morning and I wasn''t thinking straight. I meant to say that the structs were pointers.

quote:

#include <string.h>

type1 object1;
type2 object2;
type3 object3;

unsigned char array[sizeof object1 + sizeof object2 + sizeof object3];

memcpy(array, object1, sizeof object1);
memcpy(array + sizeof object1, object2, sizeof object2);
memcpy(array + sizeof object1 + sizeof object2, object3, sizeof object3);



I''m not sure how memcpy works; however, if it works the way you show, then that will be fine.

quote:
Another option might (?) be


typedef union {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType array[3];

array[0].o1 = object1;
array[1].o2 = object2;
array[2].o3 = object3;

functionYouWantToPassItTo((char *)array);


I''m also not sure why


typedef struct {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType myStuff;

myStuff.o1 = object1;
myStuff.o2 = object2;
myStuff.o3 = object3;

functionYouWantToPassItTo((char *)myStuff);


Wouldn''t work.


Oh, I get what you are saying. That may work; I''ll give it a try and respond when I get home.

-Unsuspected
-Unsuspected

This topic is closed to new replies.

Advertisement