可以任何人解释我的输出是16 [英] Can any one explain me how the output is 16

查看:39
本文介绍了可以任何人解释我的输出是16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int arr [2] [3] [2] = {{{2,4},{7,8},{3,4},},{{2,2},{2,3}, {3,4},}};

printf(%d,**(* arr + 1)+ 2 + 7);



我尝试过的事情:



我试过了,但无法得到它。可以任何一个人简要说明。答案是16

解决方案

我认为你最好自己解决这个问题,但我会给你一个提示。首先,将此视为数学问题:



16 = **(* arr + 1)+ 2 + 7



你可以从两边减去9并得到:



16-9 = **(* arr + 1)+ 2 + 7-9

7 = **(* arr + 1)



这意味着表达式得到值7,你可以看到7是在数组中。接下来的问题是,该表达式如何导致7?



从声明中你可以看到它是一个三维数组。在这个上下文中的星号是指针解引用,当没有括号与它们一起使用时,数组本质上是指针。



还有一件事,它可以帮助重写声明和初始化表达式更好地说明组织。



 int arr [2] [3] [2] = 
{
{ // 1st dim [0]
{// 2nd dim [0]
2,// 3rd dim [0],item [0] [0] [0]
4 //第3 dim [1],item [0] [0] [1]
},
{// 2nd dim [1]
7,// 3rd dim [0],item [ 0] [1] [0]
8 //第3 dim [1],item [0] [1] [1]
},
{// 2nd dim [2]
3,// 3rd dim [0],item [0] [2] [0]
4 // 3rd dim [1],item [0] [2] [1]
},
},
{{2,2},{2,3},{3,4},}
};





此时我希望你有足够的能够推断答案。


那是指针算法。 />
假设你有64位机器, int 是4字节长:

* arr type是指向int 指针的指针。在64位计算机上添加1个增量,按 8 字节。另一方面,int是4字节长,所以它跳到数组的第三个整数。


按照指针.....



为了便于理解,试试



int i = **(* arr + 1);

printf(% d,I + 2 + 7);

int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};
printf(" %d",**(*arr+1)+2+7);

What I have tried:

I have tried, but couldn't get it. Can any one brief it. The answer is 16

解决方案

I think it is best that you figure this out for yourself but I'll give you a hint. First, think of this as a math problem :

16 = **(*arr+1)+2+7

You can subtract 9 from both sides and get :

16-9 = **(*arr+1)+2+7-9
7 = **(*arr+1)

This means the expression results in the value 7 and you can see where 7 is in the array. The next question is, how does that expression result in 7?

From the declaration you can see it is a three-dimensional array. Asterisks in this context are pointer dereferences and arrays are essentially pointers when no brackets are used with them.

One more thing, it can help to rewrite the declaration and initialization expression to illustrate the organization better.

int arr[2][3][2]=
{
   { // 1st dim [0]
       {  // 2nd dim [0]
          2,  // 3rd dim [0], item [0][0][0]
          4   // 3rd dim [1], item [0][0][1]
       },
       {  // 2nd dim [1]
          7,  // 3rd dim [0], item [0][1][0]
          8   // 3rd dim [1], item [0][1][1]
       },
       {  // 2nd dim [2]
          3,  // 3rd dim [0], item [0][2][0]
          4   // 3rd dim [1], item [0][2][1]
       },
   },
   {{2,2},{2,3},{3,4}, }
};



At this point I hope you have enough to be able to extrapolate the answer.


That's pointer arithmetic.
Assuming you have a 64 bit machine and int are 4-bytes long:
*arr type is pointer to pointer to int. On a 64 bit machine adding 1 increments it by 8 bytes. On the other hand int are 4-bytes long so it leaps to the third integer of the array.


Follow the pointers.....

For easy understand try

int i=**(*arr+1);
printf(" %d",i+2+7);


这篇关于可以任何人解释我的输出是16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆