C++中一些容易迷惑的语法点总结

本文通过一个C++示例程序详细解析了数组、指针及多级指针的概念和使用方法,并展示了如何通过不同类型的指针访问数组元素。此外,还提供了获取基本类型变量所占内存大小的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

 

 
 

 一、基本类型变量占用的内存问题

转载于:https://www.cnblogs.com/bobodeboke/p/3384615.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值