简体   繁体   中英

pointer to 2d array

I have the following code:

int arr[2][2][2]={10,3,4,5,6,7,8,9};
int *p;
printf("%u",arr);
p=(int *)arr;
printf("%u",p);

Which outputs

64166
64164

But I would think that p and arr point to the same memory address. Why are different addresses shown?

Let's walk through the code

 int *p;
 printf("%u",p);

p is an unitialized int pointer. It is going to print out whatever is in memory.

 p=(int *)arr;
 printf("%u",p);

p now is pointing to the address of the array in memory, and prints that address.

But same code

 #include <stdio.h>

    int main()
    {

         int arr[2][2][2]={10,3,4,5,6,7,8,9};
         int *p;
         printf("\n%u",arr);
         p=(int *)arr;
         printf("\n%u\n",p);
         return 0;
    }

gives same result only.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM