2

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?

3
  • 4
    The first printout prints an uninitialized value. Commented Jul 27, 2012 at 12:26
  • 1
    I ran the the same piece of code, the result is : 3216608828,3216608828 ! Commented Jul 27, 2012 at 13:09
  • thnks for clearing the doubt... Commented Jul 27, 2012 at 13:15

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

oh sorry,i made a mistake in the code... the first printf is printf("%u",arr) and since arr and p point to same value then why does the second printf show diff values...
Are you trying to print out values from the array?
Adding to this, %u is for printing an unsigned int, %p prints a pointer.
I m just trying to figure out the logic behind this.i dont want to print out values from array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.