-4
char a[5] = "hill";

I am confused... what a actually is? It is said to be of type char*. It is a pointer, its value must be an address of the first character. And a itself must have an address. Surely these two addresses have to be different as a is of type char* and the address it hold points to char.

std::cout << (int)&a << ' ' << (int)a << std::endl;

Oh well, the exact same address is printed out. Please explain me, what is happening here

jpw
  • 44,361
  • 6
  • 66
  • 86
user3600124
  • 829
  • 1
  • 7
  • 19
  • 3
    `a` is an array, not a pointer. It can be used as a pointer to the first element because of a thing called *array decay*. This was answered countless times already... – HolyBlackCat Jun 21 '17 at 15:27
  • 2
    Here's the link: [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – HolyBlackCat Jun 21 '17 at 15:29
  • a holds a number (seems like it when printed out with std::cout). How is a number an array? Looks like an address to me and nothing more – user3600124 Jun 21 '17 at 15:30
  • Read the link please. `(int)a` makes `a` decay from `int [5]` to `int *` (pointer to the first element), which is then converted to int. – HolyBlackCat Jun 21 '17 at 15:31
  • @user3600124 It's clear from your comments you did not read the provided link. `a` will often decay to a pointer when it's used where a pointer is expected. In *those cases* it will behave as a pointer. In every other case, it's an array. Your `cout` statement is one of those cases that will treat it as a pointer. – François Andrieux Jun 21 '17 at 15:31
  • Because `char variable_name[size];` is the official syntax of a char array declaration in C++? – Daniel Daranas Jun 21 '17 at 15:31

1 Answers1

0

I am confused... what a actually is? It is said to be of type char*

If you define an array like you do: char a[5] = "hill;". Then the following holds:

1. a     implicitly decayed to char *: decayed to a pointer to the first element
2. &a[0] is of type char *:            pointer to the first element
3. &a    is of type char (*)[5]:       pointer to an array which starts at the first element

All of them points to the same memory address but they are of a different type. Note that in case 1 the array name will be decayed.

Oh well, the exact same address is printed out. Please explain me, what is happening here

That means if you print out the address of either a or &a it will be the same. Also remember that if you want to print an address you cast the pointer to a void* and not just to an int.

It is a pointer, its value must be an address of the first character. And a itself must have an address. Surely these two addresses have to be different as a is of type char* and the address it hold points to char.

With your assumption that the a points implicitly to the first element you're right. Also this variable has an address where the value that point to something is stored.


Edit #1:

Proof of the above:

Becaue several comments challenged, what I explained about a, &a[0] and &a here is the proof. Therefore I wrote this little not compiling C code:

int main()
{
    char a[5];
    double b;

    /* the following fails */
    b = a;     /* decay to char * */
    b = &a[0]; /* char *          */
    b = &a;    /* char (*)[5]     */
}

The compiler will fail and output errors and the expected type.

../src/SO_Test_C.c:6:7: error: incompatible types when assigning to type ‘double’ from type ‘char *’
     b = a;
       ^
../src/SO_Test_C.c:7:7: error: incompatible types when assigning to type ‘double’ from type ‘char *’
     b = &a[0];
       ^
../src/SO_Test_C.c:8:7: error: incompatible types when assigning to type ‘double’ from type ‘char (*)[5]’
     b = &a;
       ^

sizeof():

Here is what the different sizeof() operations are returning:

sizeof(a)     --> Array size:   5
sizeof(&a[0]) --> Pointer size: 8 (64bit) / 4 (32bit)
sizeof(&a))   --> Pointer size: 8 (64bit) / 4 (32bit)
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47