0

I'am pretty amateur in C, please forgive me if this question is silly.

This is memset implementation in GCC took from here:

PTR
memset (PTR dest, register int val, register size_t len)
{
  register unsigned char *ptr = (unsigned char*)dest;
  while (len-- > 0)
    *ptr++ = val;
  return dest;
}

I would like to know why memset returns pointer that was passed thru parameter, is not changed by function.

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • so that you can chain `memset()` in between different function calls: `memset(memset(&foo, 0x55, 1000), 0xAA, 1000); /*reset/set all bits of foo*/` ... not saying this is a good idea, though – pmg Oct 05 '20 at 19:51
  • Does it use more memory/stack than it would use if there was just void? – Kamil Oct 05 '20 at 20:09
  • No it does not. – 0___________ Oct 05 '20 at 20:33
  • You can treat the function as if it returns void. You lose nothing by making it return something and may eventually gain some freedom on how you set up your code. – pmg Oct 05 '20 at 20:44

0 Answers0