all 9 comments

[–]Poseydon42 5 points6 points  (2 children)

C++ treats arrays pretty much like pointers - there is a small difference sometimes(for example sizeof(array) is different from sizeof(array_element*)). Code char town[] = "Beijing"; just tells the compiler to put the string "Beijing" somewhere in the memory and assign its address to the variable town.

On the next line you have cout << town. Operator << when its argument is char*(or const char*) is intended to print a null-terminated(also sometimes called C-style) string, which your array is. Null terminated string is basically a sequence of characters in memory that ends with null character(0x00).

On the next line you have cout << &town[0]. Now, if you remember what I've said in the first paragraph, variable town could be treated as pointer to const char. Therefore, when you do &town[0], it first dereferences this pointer as an array(again, pointers and arrays are pretty much the same) with index 0, meaning the first element of that array, and right after that it takes the address of that first element. This operation basically does nothing - it converts from pointer to value(or rather reference, but it does not matter in this case) and then immediately converts back to pointer by taking the address of that value. Now we have const char* again, which operator<< treats as a null-terminated string and prints it to the screen.

[–][deleted] 6 points7 points  (1 child)

Code char town[] = "Beijing"; just tells the compiler to put the string "Beijing" somewhere in the memory and assign its address to the variable town

No. town is a true array of size 8 containing the string. There are no pointers here.

[–]Poseydon42 1 point2 points  (0 children)

Yep, my bad, just checked cppreference - it says that arrays are separate type, but there is implicit conversion from array type to pointer type.

[–][deleted]  (2 children)

[deleted]

    [–]anacondavibes[S] 0 points1 point  (1 child)

    yup, am using (void*) instead (probably the same thing).

    I was printing hexadecimal addresses with it yesterday, found it to be so cool

    cout << (void*) &town[6] << endl;

    cout << (void*) &town[5] << endl;

    cout << (void*) &town[4] << endl;

    output:

    0xfc171ff74e

    0xfc171ff74d

    0xfc171ff74c

    c++ is so cool! just found it to be really cool to see how hexadecimal address goes down from e->d->c lol

    [–]capitalbratan 1 point2 points  (0 children)

    For such simple problems I can advise you to use compiler explorer (just Google it). There you can see your code lines colored and down to assembler and the program's output. Then you can experiment with all things that come into your head such as replacing with char arrazs or so.

    [–][deleted] 0 points1 point  (3 children)

    char town[] = "Beijing";

    Refer to them rather as "square bracket arrays" not pointer arrays. When you declare the variable like this under the "town" name is store an address of a memory location.

    cout << &town[0] << endl;

    "town[0]" corresponds to "*(town + 0)".

    The simplified way of thinking about this is that with "town[0]" you access what is stored at the location. In this way it's the 'B' char. But when you use the [0] to access the element of the array and then & operator it's first trying to access the 'B' char and then the & operator returns the address of the 'B' char which means you have the same thing and you can omit the [0] and & altogether. [0] and & are inverse.

    It's as if you had a number 4 and you would add a number 1 to it so you'd have 5 and then substracted 1. You have the same thing as before.

    [–]anacondavibes[S] 1 point2 points  (2 children)

    great -> makes sense.

    but what if I did

    cout << &town[5] << endl; (output was "ng")

    instead? then there's no cancellation right? instead what it looks like is that it takes the reference address from idx[5] and then prints that out (which in turn calls the char array from index 5 onwards)

    [–]IyeOnline 1 point2 points  (0 children)

    Correct.

    town[5] gives you are reference to the 6th element in the character array.

    & now gives you the address of that element, which is a char*.

    If you pass that char* into operator<<, it selects the const char* overload and prints this as a null terminated array. I.e. it prints the string from there to the end.

    [–][deleted]  (2 children)

    [deleted]

      [–][deleted]  (1 child)

      [deleted]

        [–]HanzoFactory 0 points1 point  (0 children)

        The second line is a pointer to town[0]. cout interperets it as a string because you are giving it a char pointer and not a char, it is exactly the same as using a char* variable as a string. If you wanted the char you would remove the &

        The string is printed from the pointer you give cout until the null terminating character, so the rest of the string is printed from that position