当前位置:网站首页>局部变量的数组初始化问题

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

2022-07-06 23:08:00 疯疯癫癫才自由

 当数组不是全局变量时:
            如果数组大小是变量,则数组初始化为0时,元素的值也许不是0;
            如果数组大小是常量,则数组初始化为0时,元素的值是0;

/**
    当数组不是全局变量时:
            如果数组大小是变量,则数组初始化为0时,元素的值也许不是0;
            如果数组大小是常量,则数组初始化为0时,元素的值是0;
*/


#include <iostream>
using namespace std;
const int maxn=10;
int main()
{
    cout << "数组大小是变量:\n";
    cout << "输入两个值:\n";
    int n, m;
    cin >> n >> 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] << ' ';
}

原网站

版权声明
本文为[疯疯癫癫才自由]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51825761/article/details/125586439