How to use memset for initializing bufffers with values other than 0?

c

Solution

`memset` initializes bytes, not data types, to a value. So for your example…

int buff[1000] 
memset(buff, 5, 1000 * sizeof(int));

… if an `int` is four bytes, all four bytes will be initialized to 5. Each integer will actually have a value of `0x05050505 == 84215045`, not `5` as you're expecting.

If you would like to initialize each integer in your array to 5, you'll have to do it like this:

int i;
for(i = 0; i < 1000; i++)
    buff[i] = 5;

Problem

``` int buff[1000] memset(buff, 0, 1000 * sizeof(int)); ``` will initialize buff with o's But the following will not initialize buff with 5's. So what is the way to achieve this using memset in C (not in C++)? I want to use memset for this purpose. ``` int buff[1000] memset(buff, 5, 1000 * sizeof(int)); ```

Original source

Related problems