请解释的区别 [英] Please explain the difference

查看:88
本文介绍了请解释的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

我有一个程序大约2-D阵列


  
  

基地ADRESS是 8678


 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
主要()
{
 INT ARR [3] [3] = {
                 {} 83,8,43,
                 {} 73,45,6,
                 {} 34,67,9
                 };
的printf(%d个,&安培;常用3 + 1); //指向8696
的printf(%d个,编曲+ 1); //指向8684
返回0;
}


  

之间有什么区别改编+ 1 &放大器;常用3 + 1



解决方案

好吧,他们是不同的东西。 改编在大多数情况下衰减到的一个指针数组的第一个元素 - 这意味着一个指向你的二维数组的第3件行:键入 INT(*)[3] 改编+ 1 ,然后,指向数组中的第二排。

&放大器;改编是数组本身(类型 INT(*)的地址[3] [3] ),从而&放大器;常用3 + 1 指向的内存刚刚过去的二维数组整体的结束

您可以通过打印不同轻易确认此行为。具体来说,打印偏移到新的指针,而不是值本身将有助于清楚的事情了。从程序从这些报表打印输出:

 的printf(%LD \\ N(使用intptr_t)(安培; ARR + 1) - (使用intptr_t)ARR);
的printf(%LD \\ N(使用intptr_t)(ARR + 1) - (使用intptr_t)ARR);

将在小数偏移量&放大器;改编+ 1 分别改编+ 1 。下面是测试运行的输出我只是做了:

  36
12

36相匹配:3行×3列的每个条目×4字节= 36个字节。如此做12:1行×3列的每个条目×4字节= 12字节

请注意 - 您还使用%d个,这是不对的打印指针。你或许应该使用%P

i have a program about 2-D arrays

base adress is 8678

#include<stdio.h>
#include<conio.h>
main()
{
 int arr[3][3]={
                 {83,8,43},
                 {73,45,6},
                 {34,67,9}
                 };
printf("%d ",&arr+1);  //points to 8696
printf("%d ",arr+1);   //points to 8684
return 0;
}  

what is the difference between arr+1 and &arr+1?

解决方案

Well, they're different things. arr decays in most contexts to a pointer to the first element of your array - that means a pointer to the first 3-element row in your 2D array: type int (*)[3]. arr + 1, then, points to the second row in the array.

&arr is the address of the array itself (type int (*)[3][3]), so &arr + 1 points to memory just past the end of the entirety of your 2D array.

You can confirm this behaviour easily by printing differently. Specifically, printing the offsets to the new pointers rather than the values themselves will help clear things up. The output from your program from these print statements:

printf("%ld\n",(intptr_t)(&arr+1) - (intptr_t)arr);
printf("%ld\n",(intptr_t)(arr+1) - (intptr_t)arr);

Will be the decimal offsets to &arr+1 and arr+1 respectively. Here's the output from a test run I just made:

36
12

36 matches up: 3 rows × 3 columns × 4 bytes per entry = 36 bytes. So does the 12: 1 row × 3 columns × 4 bytes per entry = 12 bytes.

Note - you're also printing pointers using %d, which is wrong. You should probably be using %p for that.

这篇关于请解释的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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