您的位置:首页 > 其它

如何初始化二维数组

2015-08-04 00:12 225 查看
If your 2D array has static storage duration, then it is default-initialized to zero, i.e., all members of the array are set to zero.If the 2D array has automatic storage duration, then you can use an array initializer list to set all members to zero.
int arr[10][20] = {0};  // easier way
// this does the same
memset(arr, 0, sizeof arr);
If you allocate your array dynamically, then you can use
memset
to
set all bytes to zero.
int *arr = malloc((10*20) * (sizeof *arr));
// check arr for NULL

// arr --> pointer to the buffer to be set to 0
// 0 --> value the bytes should be set to
// (10*20*) * (sizeof *arr) --> number of bytes to be set
memset(arr, 0, (10*20*) * (sizeof *arr));


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: