There is the following f-I.

int main() { bool arr[6]; for (int i = 0; i < 6; i++) { cout << &arr[i] << " "; } cout << endl; char arr2[6]; for (int i = 0; i < 6; i++) { cout << &arr2[i] << " "; } cout << endl; return 0; } 

Why, when an array of char displays incomprehensible characters, and not hexadecimal numbers?

    2 answers 2

    cout << c ; where с is of type char* , it is assumed that с is a null-terminated sishnaya string. To output an address of type char* , you need to cast this type to void* , for example: cout << (void*)c;

    • Those. it can be said that this is a char type; With other types, everything is fine? - usk-dima
    • Yes, this is a char * feature, with pointers to another type everything works fine. - Janycz
    • this is a legacy of pure si. Add (void*) and in most cases everything will be as you expect. Perhaps (this needs to be checked) there will be problems with the addresses of the functions of the classes (some call this "methods", but this is not correct - there are no methods in c ++). - KoVadim
    • one
      In pure C, to print the address, use printf ("% p", & arr2 [i]); and of course, we see no problems with types (created by the C ++ class ostream). - avp

    I add to the above - this is how you can get the addresses of the cells of the array of type char:

     #include <iostream> using namespace std; int main(){ bool arr[6]; for (int i = 0; i < 6; i++) { cout << &arr[i] << " "; } cout << endl; char arr2[6]; for (int i = 0; i < 6; i++) { cout << &arr2 + i << " "; // получаем адреса с помощью адресной арифметики } cout << endl; return 0; } 
    • Yes, it’s only worthwhile to note that with a two-dimensional array this doesn’t work) - usk-dima
    • one
      @ usk-dima and why not roll, the memory is linear, you just need to think, as it seems to me. - perfect
    • Of course you can, @perfect. For arrays of any dimension. After all, all the elements are placed in memory sequentially, one after another. Only it is necessary to know the dimension for each of the measurements (it is possible, except for the last (or the first, from which end to look)). If you draw a little, then write the formula easily. - avp