爲指針數組賦初值:
#include <iostream> #include <cstring> using namespace std; int main() { int *p = new int[5]; memset(p,0,5*sizeof(int)); for( int i = 0 ; i < 5 ; i ++ ) { cout << *(p + i) << endl; } delete[] p; return 0; }
memset : 可以理解爲 Memory Set
第一個參數 : 數組的地址
第二個參數 : 初始值
第三個參數 : 總的字節數
值得注意的是,如果爲值類型賦初值 只能是0或者-1
另外需要引入 : cstring
釋放數組指針 : delete[] p -> 需要在delete後加 []