On Tue, Nov 28, 2006 at 11:36:19PM -0800, Ian Lance Taylor wrote:
> Or you can use constructor expressions to make this slightly more
> elegant, though I retain the assumptions about type sizes:
> 
> char *foo1(char* buf){
>   memcpy(buf, (char[]) { 42 }, 1);
>   buf += 1;
>   memcpy(buf, (short[]) { 0xfeed }, 2);
>   buf += 2;
>   memcpy(buf, (int[]) { 0x12345678 }, 4);
>   buf += 4;
>   memcpy(buf, (int[]) { 0x12345678 }, 4);
>   buf += 4;
>   return buf;
> }

Or even use mempcpy to make it even more compact:

char *
foo1 (char *buf)
{
  buf = mempcpy (buf, (char[]) { 42 }, 1);
  buf = mempcpy (buf, (short[]) { 0xfeed }, 2);
  buf = mempcpy (buf, (int[]) { 0x12345678 }, 4);
  buf = mempcpy (buf, (int[]) { 0x12345678 }, 4);
  return buf;
}

        Jakub

Reply via email to