数组a和& a的起始地址 [英] starting address of array a and &a

查看:128
本文介绍了数组a和& a的起始地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面两行中,

char a[5]={1,2,3,4,5};
char *ptr=(char *)(&a+1);
printf("%d",*(ptr-1));

这会在屏幕上打印5.而当使用a代替& a时,

This prints 5 on screen.Whereas when use a instead of &a,

char a[5]={1,2,3,4,5};
char *ptr=(char *)(a+1);
printf("%d",*(ptr-1));

此打印1

a和& a都是数组的起始地址.为什么会有这种区别?

Both a and &a are the starting address of the array.So Why is this difference?

也 char * ptr =& a + 1;

Also char *ptr=&a+1;

显示警告.

推荐答案

由于您似乎是个新手,所以让我用简单的术语向您解释,而不是进行严格的解释.

Since you seem totally new to it let me explain it to you in simple terms instead of going for the rigorous explanation.

对于上面的程序,您看到a&a将具有相同的数值,我相信这就是您的全部困惑所在.您可能想知道它们是否是同样,在两种情况下,以下都应在a之后给出下一个地址,并通过指针算法进行操作:

You see, for your program above, a and &a will have the same numerical value,and I believe that's where your whole confusion lies.You may wonder that if they are the same,the following should give the next address after a in both cases,going by pointer arithmetic:

(&a+1) and (a+1)

但是事实并非如此!数组的基地址(此处为a)与数组的地址不一样! a&a可能在数字上相同,但它们不是相同的 type . achar*类型,而&achar (*)[5]类型,即&a是指向(地址为)和大小为5的数组的指针.但是您知道的a数组第一个元素的地址.从数字上看,它们与下面使用 ^ 从图中看到的相同.

But it's not so!!Base address of an array (a here) and Address of an array are not same! a and &a might be same numerically ,but they are not the same type. a is of type char* while &a is of type char (*)[5],ie , &a is a pointer to (address of ) and array of size 5.But a as you know is the address of the first element of the array.Numerically they are the same as you can see from the illustration using ^ below.

但是当您增加这两个指针/地址(即(a+1)(&a+1))时,其运算法则完全不同.在第一种情况下,它跳转"到数组中下一个元素的地址,在在后一种情况下,它会跳过5个元素,因为这就是5个元素的数组的大小!.现在知道了吗?

But when you increment these two pointers/addresses, ie as (a+1) and (&a+1), the arithmetic is totally different.While in the first case it "jumps" to the address of the next element in the array, in the latter case it jumps by 5 elements as that's what the size of an array of 5 elements is!.Got it now?

  1 2 3 4 5  
  ^               // ^ stands at &a

  1 2 3 4 5
           ^     // ^ stands at (&a+1)

  1 2 3 4 5
  ^              //^ stands at a

  1 2 3 4 5
    ^            // ^ stands at (a+1)

以下内容将给出有关数组未指定边界的错误,因为未明确指定大小,如下所示,这意味着程序在遇到类似(&a + 1)时将不知道要跳转"到多少个元素.

The following will give an error about unspecified bound for array as not explicitly specifying the size as below means the program won't know how many elements to "jump" to when something like (&a+1) is encountered.

char a[]={1,2,3,4,5};
char *ptr=(char *)(&a+1);  //(&a+1) gives error as array size not specified.

现在将指针/地​​址递减为(ptr-1).在第一种情况下,在进入递减部分之前,您应该了解其上方的语句在将其强制转换为:

Now to the part where you decrement the pointers/addresses as (ptr-1).In the first case, before you come to the decrement part, you should know what happens in the statement above it where it is cast to type char*:

char *ptr=(char *)(&a+1);

这里发生的事情是您剥离"了(&a+1)的原始type,它是char (*)[5]类型,现在将其转换为与a相同的char*类型,即,数组的基地址.(再次注意数组的基地址和数组的地址之间的区别.因此,在上面的语句中进行强制转换和赋值之后,如下通过printf()中的减量,ptr现在给出了数组最后一个元素5之后的内存位置.

What happens here is that you "strip off" the original type of (&a+1) which was type char (*)[5] and now cast it to type char* which is the same as that of a,ie, the base address of the array.(Note again the difference between base address of an array and address of an array.So after the cast and assignment in the above statement,followed by the decrement in printf(), ptr now gives the memory location right after the last element of the array, which is 5.

  1 2 3 4 5
          ^     // ^ stands at location of 5, so *ptr gives 5

因此,在将指针ptr递减为*(ptr-1)后取消引用它时,它会按预期打印5的值.

So when you dereference the pointer ptr after decrementing it as *(ptr-1) it prints the value of 5 as expected.

现在,最后将它与打印1的第二种情况进行对比.看一下我使用符号 ^ 给出的插图.当您将a递增为a+1时,它指向数组的第二个元素,即2,并且您已将该地址分配给ptr.因此,当您将ptr递减为(ptr-1)时,它跳回一个元素,现在指向数组的第一个元素,即1.因此,在第二种情况下取​​消引用ptr会得到1.

Now finally, contrast it with the second case where 1 is printed.Look at the illustration I have given using the symbol ^. When you had incremented a as a+1, it points to the second element of the array, ie 2 and you had assigned this address to ptr.So when you decrement ptr it as (ptr-1), it jumps back one element and now points to the first element of the array ,ie 1.So dereferencing ptr in second case gives 1.

  1 2 3 4 5
  ^            // ^ stands at address of 1, so *ptr gives 1

希望这一切都清楚了.

这篇关于数组a和& a的起始地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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