局部变量的数组初始化问题

https://blog.csdn.net/qq_51825761/article/details/125586439

/**
    当数组不是全局变量时:
            如果数组大小是变量(包括比如获取某个vector的size),则数组初始化为0时,元素的值也许不是0;
            如果数组大小是常量,则数组初始化为0时,元素的值是0;
*/
#include <iostream>
using namespace std;
const int maxn=10;
int main()
{
    cout << "数组大小是变量:\n";
    cout << "输入两个值:\n";
    vector<int> aaa{1,2};
    int n, m;
    n = aaa.size();
    cin >> m;
    int temp[n][m]={0};
    for(int i=0;i<n;++i)
        for(int j=0;j<m;++j)
            cout << temp[i][j] << ' ';
    cout << "\n数组大小是const 类型的常量:\n";
    int matr[maxn][maxn]={0};
    for(int i=0;i<maxn;++i)
        for(int j=0;j<maxn;++j)
            cout << matr[i][j] << ' ';
    cout << "\n数组大小就是 common 常量:\n";
    int a[10][10]={0};
    for(int i=0;i<10;++i)
        for(int j=0;j<10;++j)
            cout << a[i][j] << ' ';
}


输出

数组大小是变量:
输入两个值:
3
0 0 7758688 0 6421927 0
数组大小是const 类型的常量:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
数组大小就是 common 常量:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
posted @ 2022-09-18 16:21  种树人  阅读(38)  评论(0编辑  收藏  举报