Работа с указателями и со строками СИ
Мне непонятно, почему, например, для вывода указателя на int, его сначала нужно разыменовать, а при использовании указателя на char для вывода строки, операция разыменовывания уже не требуется, оно и так всё красиво выводит.
int* a = new int[5];
// Здесь будет выводиться адрес памяти.
cout << a << endl;
char* c = new char[4];
strcpy(c, "sasa");
// А вот здесь уже находится сама строка
// хотя, я думал, что здесь должен был бы выводиться адрес первого символа строки.
cout << c;
Ответы (2 шт):
Автор решения: MBo
→ Ссылка
Потому что оператор << для std::cout перекрыт для множества типов. Для char* он обучен тому, чтобы при выводить C-строку по данному адресу.
Хотите адрес - приведите к void*
Автор решения: AlexGlebe
→ Ссылка
Очень много индивидуальных определений оператора <<. Для строк сделали вывод строки, а для других указателей как указатель.
http://www.cplusplus.com/reference/ostream/ostream/operator-free/
http://www.cplusplus.com/reference/ostream/ostream/operator<</
single character (1)
ostream& operator<< (ostream& os, char c);
ostream& operator<< (ostream& os, signed char c);
ostream& operator<< (ostream& os, unsigned char c);
character sequence (2)
ostream& operator<< (ostream& os, const char* s);
ostream& operator<< (ostream& os, const signed char* s);
ostream& operator<< (ostream& os, const unsigned char* s);
rvalue insertion (3)
template<class charT, class traits, class T>
basic_ostream<charT,traits>&
operator<< (basic_ostream<charT,traits>&& os, const T& val);
arithmetic types (1)
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (long long val);
ostream& operator<< (unsigned long long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
stream buffers (2)
ostream& operator<< (streambuf* sb );
manipulators (3)
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));