#include <iostream> using namespace std; int main() { //new 申請空間 //int *p = (int*)malloc(sizeof(int)); //c語言的申請用法 int *p1 = new int; //new + type 類型需要匹配 int *p2 = new int(121); //初始化一個值 char *c = new char; *p1 = 12; //寫入 cout << *p1 << endl; //讀取 int *a = new int[5]; //申請數組 memset(a,0,5*4) ; //使用函數批量初始化 全稱 memery set. c++沒有提供數組初始化,可以用函數實現。 //int *a1 = (int*)malloc(5*4); //c語言的的數組申請 //a[0] = 121; cout << a[0] << endl; //釋放內存 delete p1; //delete+指針 delete p2; delete c; delete[] a; //釋放數組,對於標准語法而言,如果不匹配釋放,結果是不確定的。 system("pause"); return 0; }
#include <iostream> using namespace std; int main() { // *號的作用 //在聲明變量的時候* 指針變量 int a = 12; int *p = &a; *p; //地址操作符 讀 寫 cout << *p << endl; *p = 123; cout << a << endl; // 乘法運算 int b = 24; int c = 12 * 21 *b; cout << c << endl; system("pause"); return 0; }