1

In this program:

#include <stdio.h>

int main()
{
    int a[2];
    int *b=&a;
    printf("%p\n",a);
    printf("%p\n",&a);
    printf("%p",b);
    return 0;
}

All printing same things!! a is a pointer to first place of our array right?! It must be saved somewhere!! so &a or b must be a pointer to a...

I'm wrong?!

BTW ive got a warning from my compiler about int *b=&a;

[Warning] initialization from incompatible pointer type [enabled by default]
amfad33
  • 74
  • 8
  • 1
    Read [Difference between `&str` and `str`, when `str` is declared as `char str[10]`?](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Jan 13 '14 at 08:36
  • 1
    And I think I have answer your question here [Inconsistency in using pointer to an array and address of an array directly](http://stackoverflow.com/questions/17661078/inconsistency-in-using-pointer-to-an-array-and-address-of-an-array-directly/17663091#17663091) It is kind of duplicate. – Grijesh Chauhan Jan 13 '14 at 08:38
  • You missed `&a[0]` :D – anishsane Jan 13 '14 at 10:54
  • @anishsane he didn't miss `[0]` but OP thinks that `&a` is address of first element...Read title of question he is asking `a` and `&a` are same?? – Grijesh Chauhan Jan 13 '14 at 11:18
  • 2
    I meant, typically, people ask differences between `a`, `&a` & `&a[0]`... OP asked only 2... – anishsane Jan 13 '14 at 11:47

1 Answers1

0

&a is int (*)[2]. i.e pointer to an array.

But int *b=&a; assigns to integer pointer. Though it points to proper address, you need to typecast to remove the warning message.

Add (int*) like.

int *b=(int*)&a;

#include <stdio.h>

int main()
{
    int a[2] = {10,20};
    int *b=(int*)&a;

    printf("\n :%d   %d\n", b[0], b[1]);
#if 0
    printf("%p\n",a);
    printf("%p\n",&a);
    printf("%p",b);
#endif
    return 0;
}

prints 10 and 20.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63