memset二维数组初始化
时间: 2023-10-16 22:03:42 浏览: 183
使用memset函数初始化二维数组时需要注意以下几点:首先,对于二维整型数组,memset函数只能将数组初始化为0或者-1,否则数组的值将会是随机数;而对于二维char数组,memset函数则可以将数组初始化为任意字符。其具体写法如下:
```
#include <bits/stdc++.h>
using namespace std;
const int m = 3;
const int n = 5;
char a[m][n]; // char数组
int main() {
memset(a, '*', sizeof(a)); // 此处的星号*可改为任意字符
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j << " ";
}
cout << endl;
}
return 0;
}
```
在上述代码中,使用`memset(a, '*', sizeof(a))`将二维char数组a初始化为*字符。注意,这里的`'*'`可以改为任意字符。运行代码后,即可将二维数组a的所有元素都初始化为相应字符。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++——C++创建动态二维数组+memset()函数初始化](https://blog.csdn.net/CSDN131137/article/details/103016793)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [使用memset()函数初始化二维数组](https://blog.csdn.net/qq_53269459/article/details/119535151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [使用memset()函数初始化二维数组时的注意事项](https://blog.csdn.net/hnjzsyjyj/article/details/112408497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文