1. 程式人生 > >C++ typedef 與define的運用

C++ typedef 與define的運用

#include <iostream>
using namespace std;
typedef char* CString;  //將字元指標重新命名為CString,此時CString就代表字元指標
#define SIDE "You are welcome to join us." //巨集定義
#define MUL(x,y) ((x)*(y)) //定義兩個數之積,必須用括號來保護表示式中低優先順序的操作符
int main(){
    int a,b,c;
    cin >>a>>b;
    c = 3*MUL(a,b);
    cout <<"c="<<c<<endl<<endl;

     CString str1,str2;
     char temp[] = "Hello World";
     cout <<"SIDE="<<SIDE<<endl<<endl;

     cout <<temp<<endl;
     cout <<"sizeof(char*)="<<sizeof(char*)<<endl;
     cout <<"sizeof(CString)="<<sizeof(CString)<<endl;
     cout <<"sizeof(temp)="<<sizeof(temp)<<endl;
     str1 = &temp[3]; //這裡取temp[3]的地址嗎?後面的為什麼都輸出了
     cout <<"str1="<<str1<<endl;

     str2 =temp; //此處又是為什麼
     cout <<"str2="<<str2<<endl;
}