Page 1 of 1

A "clean" way of doing this?

Posted: Tue Oct 16, 2007 10:36 pm
by earlz
Ok, I have ran into a bit of a problem...

ok, I have a huge array
char big[1024];

ok, so now, I want to write an int to element 5
well, there could be (int)big[5]=my_int;
but that would actually write to big[5*sizeof(int)]
so, exactly how do you go about doing this easily and without temporary variables?
like, basically, a "clean" way to do this:

Code: Select all

char bah[256];
int *bahi=(void*)&bah[5];
*bahi=0x220;


Posted: Tue Oct 16, 2007 11:31 pm
by os64dev
if you wish to put and int at the fifth character in the buffer:

Code: Select all

*(int *)&big[5] = 0x220
if you wish to put a value in the fifth integer in the buffer:

Code: Select all

((int *)big)[5] = 0x220;

Posted: Wed Oct 17, 2007 11:48 am
by earlz
ok, one more...
how about

Code: Select all

uint32_t bah[256];
uint16_t *sbah=(void*)&bah;
sbah[1]=0x220; //as in, the 2nd word, or the 4th byte, or the lower half of the first dword

edit:
Ok, so basically what operator[] when it returns a reference...you return a pointer to where the rvalue should be put in...right?

Well, I am having problems with invalid initializations with temporaries...basically, I am trying to make it so that my_class16[1]=5; will do my_class32[1]=(my_class32[1]&0xFFFF0000)+5;

how should I go about doing that with operator []!?