Code:int arr[512]; int val = 5; /* how i use memset() to set all the elements of arr[] to val? */
Code:int arr[512]; int val = 5; /* how i use memset() to set all the elements of arr[] to val? */
I dont think you can with memset. You can set each BYTE of that block of memory to 5 but not each element (which is made up of 4 bytes)
Code:for(unsigned int i = 0; i < sizeof(arr) / sizeof(*arr); ++i) arr[i] = val;
yeah! i had to do the obvious:
but since assembly has the instructions for byte,word and dword:Code:void fill_grid(int* grid, int nr, int nc, int val) { int i; for (i=0; i<(nr*nc); i++) { grid[i] = val; } }
stodb, stosw, stosd
and memset works very fast, i thougt may be there is something to set ints as well!
leave it to compiler's optimizerbut since assembly has the instructions for byte,word and dword:
stodb, stosw, stosd
and memset works very fast, i thougt may be there is something to set ints as well
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler