memcpy() memcat()?
Is there a memcat() version of memcpy() or something?
I want to copy the combined memory of three variables into one variable (different var types).
How can i do that?
I don''t know about a memcat() function.
What kinds of variables are you trying to copy from and to? If you''re concatenating character arrays, you can use strcat(). Otherwise, I would probably copy the data of a variable at a time.
My Wonderful Web Site (C++ SDL OpenGL Game Programming)
I am a signature virus. Please add me to your signature so that I may multiply.
What kinds of variables are you trying to copy from and to? If you''re concatenating character arrays, you can use strcat(). Otherwise, I would probably copy the data of a variable at a time.
My Wonderful Web Site (C++ SDL OpenGL Game Programming)
I am a signature virus. Please add me to your signature so that I may multiply.
use three memcpy()''s but use pointer arithmatic(sizeof() comes to mind..) to copy them into right places
quote:Original post by Pepe
int i;
float b;
char c;
struct Xxx {
int i;
float b;
char c;
};
Xxx x;
x.i = i;
x.b = b;
x.c = c;
If it was that simple, I wouldn''t have asked. I the vars are totally different and I need a straight memory copy.
quote:
What kinds of variables are you trying to copy from and to?
Im puting a struct in an unsigned char. (it has to be unsigned char because it is directX)
quote:use three memcpy()''s but use pointer arithmatic(sizeof() comes to mind..) to copy them into right places
Huh? Either I am confused or you dont know what memcpy does.
To normally copy one thing to another, I could write:
memcpy(first, second, sizeof(second));
however, I want to add several things into first; that is my question.
quote:Original post by Pepe
int i;
float b;
char c;
struct Xxx {
int i;
float b;
char c;
};
Xxx x;
x.i = i;
x.b = b;
x.c = c;
If it was that simple, I wouldn''t have asked. I the vars are totally different and I need a straight memory copy.
quote:
What kinds of variables are you trying to copy from and to?
Im puting a struct in an unsigned char. (it has to be unsigned char because it is directX)
quote:use three memcpy()''s but use pointer arithmatic(sizeof() comes to mind..) to copy them into right places
Huh? Either I am confused or you dont know what memcpy does.
To normally copy one thing to another, I could write:
memcpy(first, second, sizeof(second));
however, I want to add several things into first; that is my question.
-Unsuspected
If you need an unsigned char array from a struct, you might be able to take a pointer to the struct and cast it to unsigned char.
This assumes that there is no padding between the various members.
struct TheStruct a = {1, 2, 3}struct TheStruct* b = a;unsigned char* c = (unsigned char*)b;
This assumes that there is no padding between the various members.
quote:Original post by rotos
If you need an unsigned char array from a struct, you might be able to take a pointer to the struct and cast it to unsigned char.struct TheStruct a = {1, 2, 3}struct TheStruct* b = a;unsigned char* c = (unsigned char*)b;
This assumes that there is no padding between the various members.
That would only put one struct in it, though. I need to copy several structs to the unsigned char.
my unsigned char also isn''t a pointer, fyi.
-Unsuspected
Also, it has to be a memory copy; hence, adding the structs algebraicly (sp?) won''t give me the desired results. I need to put in the unsigned char all the structs, one after another.
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++
I''ll assume the second. So you have something like
Another option might (?) be
I''m also not sure why
Wouldn''t work.
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++
I''ll assume the second. So you have something like
#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);
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement