0

As I have learnt,array name acts like a pointer to first element.But:

int c[]={0,1,2};
printf("%d \t %d",c,&c[0]); //Different values,Why?

Also then why *c=0?

3
  • *c is the same as c[0] and you have set the first element of c to be 0 so that explains that part. Commented May 5, 2012 at 19:15
  • 5
    You should print pointers with %p, not %d. Sometimes it doesn't matter much, on some platforms it does a lot. Commented May 5, 2012 at 19:16
  • 4
    This is undefined behaviour. &c[0] is an int*, but %d expects an int. You should cast the pointer to void* and use %p, or (assuming your platform supports this) cast to uintptr_t and use one of the macros from <inttypes.h>. Commented May 5, 2012 at 19:17

1 Answer 1

11

Just a guess: you're on a platform with 64-bit pointers and 32-bit int. Your code passes two pointer values to printf, which then interprets these as int values; that might print the two halves of a 64-bit pointer as two separate integers.

You should print pointers with %p, not %d, after casting them to void*.

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

Comments

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.