A "clean" way of doing this?

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

A "clean" way of doing this?

Post 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;

User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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;
Author of COBOS
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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 []!?
Post Reply