怀疑阵列的地址? arr和& arr的使用之间的差异 [英] doubt in address of array? difference between usage of arr and &arr

查看:117
本文介绍了怀疑阵列的地址? arr和& arr的使用之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main()
{
 int arr[]={1,2,3}; 
printf("%u %u",arr,&arr); 
printf("%u %u",arr+1,&arr+1);
}



第一个声明我得到arr和& arr的相同地址。

第二个它给出了不同的y?

使用arr和& arr的区别是什么?

可以解释一下吗?


first statement i am getting same address for arr and &arr.
2nd it gives different y?
wats the difference in using arr and &arr?
can any one explain me?

推荐答案

arr & arr 始终保持相同的地址(数组的名称是指向数组第一项的指针,也是& arr [0] 给出相同的地址。)

至于指针数学,的性质(类型) arr & arr 不同:(arr + 1)跳转到下一个(数组的2 nd )项,而(& arr + 1 )跳转到数组的末尾(编译器进入计算数组大小)。



请注意您的代码应该是:

arr and &arr always hold the same address (the name of an array is a pointer to array first item, also &arr[0] gives the same address).
As for pointer math, the nature (the type) of arr and &arr is different: (arr+1) jumps to the next (the 2nd) item of the array, while (&arr+1) jumps to the end of the array (the compiler takes into account the array size).

Please note your code should be:
#include <stdio.h>

int main()
{
  int arr[]={1,2,3};
  printf("%p %p\n",arr,&arr);
  printf("%p %p\n",arr+1,&arr+1);
  return 0;
}










引用:

你能说我使用** p吗? 。令人困惑。提前谢谢。

can you say me the use of **p? . its confusing. thankz in advance.



可以使用双指针,例如,在锯齿状数组实现中:


A double pointer could be used, for instance, in jagged array implementation:

 #include <stdio.h>
 #include <stdlib.h>
 #define N 3
int main()
{
  int ** pp;
  int size[N] = { 2,3,1 } ;
  pp = (int**) malloc( N * sizeof(int *));

  int n,k, count=0;

  for (n=0; n<N; ++n)
  {
    pp[n] =(int *)  malloc( size[n] * sizeof(int));
    for (k=0; k<size[n]; ++k, ++count)
      pp[n][k] = count;
  }

  for (n=0; n<N; ++n)
  {
    for (k=0; k<size[n]; ++k)
      printf("pp[%d][%d] = %d\n", n, k, pp[n][k]);
    printf("-------------\n");
  }


  for (n=0; n<N; ++n)
  {
    free(pp[n]);
  }
  free (pp);

  return 0;
}


这篇关于怀疑阵列的地址? arr和&amp; arr的使用之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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