#include <iostream>
using namespace std;
int main(){
int *a = new int[5];
int *b = new int[5]();
int *p = a;
int *q = a + 5;
int *r = b;
int *s = b + 5;
while( p != q )
cout << *(p++) << " ";
cout << endl;
while( r != s )
cout << *(r++) << " ";
cout << endl;
int c[5];
static int d[5];
p = c;
q = c + 5;
r = d;
s = d + 5;
while( p != q )
cout << *(p++) << " ";
cout << endl;
while( r != s )
cout << *(r++) << " ";
cout << endl;
return 0;
}
|
Output:
-1819044973 -1819044973 -1819044973 -1819044973 -1819044973
0 0 0 0 0
-1081662640 -16121856 -1081662696 134538581 134538432
0 0 0 0 0
|
int *b = new int[5]();
只需在後面加上"( )"即可將動態配置的陣列之元素值初始為0, 另外, 因為在此scope的陣列a並非static or extern, 所以並不會自動初始化值為0 (初值為garbage)
int c[5]; static int d[5];
c之初值為garbage, d是static, 會自動初始化其元素值為0
Note:
*(p++)等同於
*p++因為++的優先權比*高
沒有留言:
張貼留言