ghostrider
ghostrider

Reputation: 2238

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?

Upvotes: 2

Views: 156

Answers (2)

Jeyaram
Jeyaram

Reputation: 9504

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.

enter image description here

Upvotes: 2

Brian
Brian

Reputation: 7156

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.

Upvotes: 2

Related Questions