2
int a[10];

Now here &a and a is same, I don't clearly understand logic why is it so. Can I say that value of a is same as &a and *(a) is a[0]. (But how a is not a pointer). In case of dynamic array things are clear to me.

int *da = new int[10];

value of da is base address, &da gives the address where pointer is stored, and we deference da to reach da[0].

curiousgeek
  • 903
  • 11
  • 18
  • 4
    "*Now here `&a` and `a` is same*" - that's not true. The first one is a pointer to array of 10 elements and the second one is the name of an array, which, for exaplme in case of comparisons, decays to the pointer to its first element – Fureeish Oct 18 '18 at 08:48
  • @Fureeish cout< – curiousgeek Oct 18 '18 at 08:51
  • 4
    `std::string str = "1"; int x = 1; std::cout << str << ' ' << x;` will also give the same output, but that does not mean they are the same – Fureeish Oct 18 '18 at 08:51
  • @Fureeish I still don't understand what are you trying to say, I mean how should I relate &a and a to your analogy. – curiousgeek Oct 18 '18 at 08:53
  • 1
    I explained that in my comment. `&a` is a pointer to array. A pointer to array physically points to the array's first element, but it's not a *regular* pointer to the array's first element. `a` is the name of an array, which in most cases, due to [array decaying](https://stackoverflow.com/questions/1461432/what-is-array-decaying), gets converted to a pointer to its first element. Outputting those two using `std::cout` will produce the same value, but fundamentally they are different - you can't compare them and you can't treat them as being of the same type – Fureeish Oct 18 '18 at 08:55
  • @curiousgeek: `int (*)[10]` vs `int[10]` for `&a` vs `a` (which both decay to `int*` in your usage case). – Jarod42 Oct 18 '18 at 08:56
  • @Fureeish I didn't knew that there is something like array decaying. That's what caused the confusion. It's clear now thanks! – curiousgeek Oct 18 '18 at 09:04

2 Answers2

6

They are not the same.

a is an int[10] type; an array.

&a is a pointer to an int[10] type.

In certain situations (passing to functions, when used in arithmetic expressions), a decays to an int* type. You can then use pointer arithmetic to reach other elements of the array. That could be what is causing your confusion.

da is an int* type.

The only thing that decays to itself is a function pointer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

well 'a' and '&a' are definitely not the same

int a[10] takes up a specific block from memory to allocate exactly 10 integers and this block is called a.

'&a' refers to the memory address of this memory block of 10 integers you made. it's like you have a hotel room (the array of 10 elements) in your hotel. And (your memory address) '&a' refers to your room number, your address.

now saying for example int *p = &a means I'm making an integer pointer 'p' that can only point to integers, and i'm giving it the memory address of 'a' (the array of integers) so it can point to it.

so :

int a[10] is your hotel room;

&a is your room number;

*p = &a is the key chain attached to your room number (ur keychain points to ur room number, your address);

this is why sometimes we use : int *da = new int[10]; to immediately allocate in memory a space for 10 integers and directly point a pointer (in this case da) to it.

Hope this helps.

SeDak
  • 21
  • 1