I can not understand

char *name[] = { "la la","la" }; cout << name << endl; // этот выдаёт адрес первого элемента char *name1 = "la la"; cout << name1 << endl; // а этот самого "la la" 

Why does the first version produce an address, rather than a full string literal "la la"? After all, cout overloaded, so that instead of the address it gives the full value until it meets \0 .

  • one
    Not cout overloaded, but operator<< for std::ostream . - αλεχολυτ
  • poniatno budu znat. - Beso Poladishvili

2 answers 2

Because in fact, the name is a pointer to an array, not a pointer to char . Want to display the first element of the array - write

 cout << name[0] << endl; 
  • name is an array, not a pointer to an array. In the string cout << name , an implicit transformation of the array to a pointer to the first element of the array occurs, i.e. to type char ** , which is then implicitly converted to const void * - wololo

In the case of char *name1 , name1 is a pointer to char , i.e. line, which is displayed.

And in the first case, char *name[] , name is an array of pointers to strings. Therefore, the address of this array is displayed.

  • balshoi spasibo. - Beso Poladishvili