C++中一些容易迷惑的语法点总结
#include<iostream> #include<cstring> using namespace std; int main(){ int a[2][3]={{1,2,3},{4,5,6}}; int *p=a[0]; int (*pp)[3]=a; int *ppp[2]={a[0],a[1]}; //the step is 3*4 cout<<"a:"<<a<<endl; cout<<"a+1:"<<a+1<<endl; //the method to visit item cout<<"a[0][0]"<<a[0][0]<<endl; //the step is 4 cout<<"a[0]:"<<a[0]<<endl; cout<<"a[0]+1:"<<a[0]+1<<endl; //the method to visit item cout<<"a[0][0]:"<<a[0][0]<<endl; //the step is 4 cout<<"*p=a[0] p:"<<p<<endl; cout<<"p+1:"<<p+1<<endl; //the method to visit item cout<<"*(p+1):"<<*(p+1)<<endl; //the step is 3*4 cout<<"(*pp)[3]=a pp:"<<pp<<endl; cout<<"pp+1:"<<pp+1<<endl; //the method to visit item cout<<"*(*(pp+1)+1)"<<*(*(pp+1)+1)<<endl; //the method to visit item cout<<"*(ppp[i]+j) or ppp[i][j]:"<<ppp[1][2]<<" or "<<*(ppp[1]+1)<<endl; return 0; }
int getMemorySize(){ cout<<"char:"<<sizeof(char)<<endl; cout<<"bool:"<<sizeof(bool)<<endl; cout<<"int:"<<sizeof(int)<<endl; cout<<"short int:"<<sizeof(short int)<<endl; cout<<"long int:"<<sizeof(long int)<<endl; cout<<"float:"<<sizeof(float)<<endl; cout<<"double:"<<sizeof(double)<<endl; cout<<"int*"<<sizeof(int*)<<endl; cout<<"double*"<<sizeof(double*)<<endl; return 0; }
一、基本类型变量占用的内存问题