1

The following code snippet proofs that both are the same:

int a[4]; printf("a: %p\n&a: %p", a, &a); "0x12345678" "0x12345678" 

But the compiler will warn in case of:

int a[4], *p; p = &a; 

assignment from incompatible pointer type

Which pointer type does &a have ?

3
  • 1
    @haccks sir, in this particular question, casting the arguments to void * is a required part. I would like to ask you for a reconsideration of marking this as dupe. Thank you. :-) Commented Jul 2, 2015 at 11:11
  • @SouravGhosh; Check it again :) Commented Jul 2, 2015 at 11:35
  • @haccks Thanks for the edit, but that does not invalidate my earlier comment, either. :-) Commented Jul 2, 2015 at 11:37

1 Answer 1

3

To be correct, first, your print statement should look like

 printf("a: %p\n&a: %p", (void *)a, (void *)&a); 

because %p expects a void * argument. Please, note, printf() being a variadic function, implicit conversion (cast) won't take place, so, the casting is required.

Now, that said, a being an array,

  • a is of type int [4]
  • &a is if type int (*)[4]

(but, both will return the same address, FWIW.) You can also see this answer.

OTOH, in your case, p is of type int *.

That's why your compiler warns you about the type mismatch in the assignment.

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

5 Comments

@Quentin sadly, the dupe won't address the casting part required in printf() in this case.
&a[0] a and &a all have the same address but of different types
@Gopi a is the same as &a[0] after decaying.
p=&a fires a warning (but works). Why throws p=a not a warning since int * is not int [4]. Is int (*)[4] similar to a pointer to pointer type ?
@mr.wolle I've updated my answer with a link, please check that. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.