Why is memset() incorrectly initializing int?

后端 未结 9 1137
迷失自我
迷失自我 2020-11-28 09:56

Why is the output of the following program 84215045?

int grid[110];
int main()
{
    memset(grid, 5, 100 * sizeof(int));
    printf(\"%d\", grid         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 10:21

    memset is about setting bytes, not values. One of the many ways to set array values in C++ is std::fill_n:

    std::fill_n(grid, 100, 5);
    

提交回复
热议问题