一、定義
數組的維數必須用大於等於1的常量表達式來定義
整形字面值常量、枚舉常量或者常量表達式初始化的整形const對象;
二、初始化
1、顯示初始化數組元素
*在函數體外定義的內置數組,其元素均初始化為0;
*在函數體內定義的內置數組,其元素無初始化;
*不管數組在哪裡定義,如果其元素為類類型,則自動調用該類的默認構造函數進行初始;如果該類沒有默認構造函數,則必須為該數組的元素提供顯示初始化
2、特殊的字符數組
3、不允許數組直接復制和賦值
// SHUZU.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
int get_size()
{
static int i = 0;
i = i + 1;
return i;
}
class testClass
{
public:
protected:
private:
int a;
char* p;
};
int atest[10];
testClass atc[10];
int _tmain(int argc, _TCHAR* argv[])
{
const unsigned buf_size = 512, max_files = 20;
int staff_size = 27;
const unsigned sz = get_size();//sz是const對象,但是他的值要多運行時調用get_size才能知道
char input_buffer[buf_size];//buf_size是const常量
string fileTable[max_files + 1];//ok max_files是const常量,max_files + 1在編譯時候就能算出是21
// double salaries[staff_size];//error non const variable
// int test_scores[get_size()];//error not const expression
// int vals[sz];//error size not known until run time
//顯示初始化數組元素
int btest[10];
testClass btc[10];
//特殊的字符數組
char ca1[] = {'c', '+', '+'};//3維
char ca2[] = {'c','+','+', '\0'};//4維
char ca3[] = "c++";//4維 null terminator added automatically
//不允許數組直接復制和賦值
int ia[] = {0, 1, 2};
//int ia2[](ia); //error
int ia3[];
//ia3 = ia; //error
return 0;
}

string[] mycheck;
mycheck=new string[36];
for(int i=0;i<36;i++)
{
mycheck[i]="1";
}
二維數組定義的一般形式如下:
類型標識符 數組名【常量表達式1】【常量表達式2】;
例如:
int a[2][3];
float b[3][10];
二維數組的初始化有兩種:
(1)分行初始化,如:
static int a[2][3]={{1,2,3,},{4,5,6}};
(2)統一初始化,如:
static int a[2][3]={1,2,3,4,5,6};