Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax help: How can I copy a struct into a charactar array? 1

Status
Not open for further replies.

skhoury

IS-IT--Management
Nov 28, 2003
386
US
Hello all,

I need some help copying the contents of a struct directly into a char array. Here is what I have as an example:

struct MyStruct{
int x; // 4 bytes
int y; // 4 bytes
char z; // 1 byte
};

MyStruct test;
test.x = 1;
test.y = 2;
test.z = "a";

Then, I would like to create a dynamic array like such:
char *array;
array = (char*)malloc(9);

How can I copy the structs contents into this array (easily). i.e. without iterating through all the members, etc.

Any help is greatly appreciated.

Thanks All!

Sam
 
hmm, yes I think that pretty much gets me in the right direction.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top