Hi,

This is the code fragment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
        int* bugs_bunny;
        bugs_bunny = (int*)malloc(3*sizeof(int));
        memset(bugs_bunny, 5, 3*sizeof(int));
        printf("bugs_bunny[0] %d\n",bugs_bunny[0]);
        bugs_bunny[0] = 17;
        printf("bugs_bunny[0] %d\n",bugs_bunny[0]);
        return (1);
}

The outcome is:

bugs_bunny[0] 84215045
bugs_bunny[0] 17

Why is the value printed for bugs_bunny[0] using memset producing garbage value while the 2nd one is printing the right result?

Thanks.

You thought that memset would put an integer 5 into each element of your array, but that isn't how it works. It is putting a 5 into each byte.

5*256*256*256 + 5*256*256 + 5*256 + 5*1 = 84215045