c指针解析

今天定义了一个多维数组

bool dp[2][500][500];

然后想吧这两个500*500的数组分别给两个指针,于是

bool **cur = dp[0];
bool **next = dp[1];

结果:

error: cannot convert 'bool (*)[500]' to 'bool**' in initialization
error: cannot convert 'bool (*)[500]' to 'bool**' in initialization

可以看出,dp[0]类型为bool (*)[500], cur类型是 bool**, 显然我需要把cur 也定义成 bool (*)[500]。于是改成下面这个样子就可以了。

bool (*cur)[500] = dp[0];
bool (*next)[500] = dp[1];

然后顺便借此翻了一下《征服c指针》这本书,稍微回顾了一下。
先上例子

int doge; //doge is int
int doge[10]; //doge is an array(size 10) of int
int doge[10][3]; //doge is an array(size 10) of array(size 10) of int
int *doge[10]; //doge is an array of pointer to int
int (*doge)[10];//doge is a pointer to array(size 10) of int
int doge(int a); //doge is a function(int a) returning int
int (*doge)(int a);//doge is a pointer to function(int a) returning int
int *doge(int a); //doge is a function(int a) returning a pointer to int

解读的时候按照优先级依次解析。

/*
比如 int doge[10][3]; 
1.doge[10]:doge是一个长度为10的数组
2.(doge[10])[3]:这个数组的每个元素是一个长度为3的数组
3. int ((doge[10])[3]):长度为3的数组的每个元素是int类型。
*/
int main(){
   int doge[10][3];
   cout << sizeof(int) << " "
        << sizeof(doge[10][3]) << " "
        << sizeof(doge[10]) << " "
        << sizeof(doge) << endl;
   return 0;
}
/*
output:
4 4 12 120
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值