Why is printing a string array output in hexadecimal?
Why does the following program print "0x2ffee4" on the console?
#include <string> #include <iostream> using namespace std; int main() { string city1[] = "toronto"; cout << city1; return 0; } The answer given by TC is correct, I would also like to mention that if you were expecting to print "toronto" on the console using cout, you would want to do this:
include <string> include <iostream> int main() { using namespace std; // string city1[] = "toronto"; // Compiler Error - Do Next Line Instead string city1[] = { "toronto" }; cout << city1[0]; return 0; } Anytime you want to initialize an array of any type during declaration, you need to use = {}; To set each element of the array, separated by commas. Have a look at this sample code:
#include <string> #include <iostream> int main() { using namespace std; string cities[] = { "New York", "Philadelphia", "Chicago", "Boston" }; // Same As Above Except Size Of Array Is Defined First Then The Elements string cities[4]; cities[0] = "New York"; cities[1] = "Philadelphia"; cities[2] = "Chicago"; cities[3] = "Boston"; unsigned index = 0; for ( ; index < 4; index++ ) { cout << cities[index] << endl; } return 0; } If you do not initialize the array when it is declared, you must specify the size of the array.
int main() { int iArray[]; // Compiler Error int iArray[] = { 4, 3, 2, 1, 6 }; // Okay and is the same as int iArray[5]; // Okay iArray[0] = 4; iArray[1] = 3; iArray[2] = 2; iArray[3] = 1; iArray[4] = 6; return 0; }
If you do not use the bracket operator with the index to send std :: cout to the console output stream, then the hexadecimal value that you get is correct, as TC already stated; it returns the address of the first index. This is why arrays and pointers are similar in c / C ++ (they don't match, but they behave almost like each other). The main difference from arrays is that they are constant in size, the size of which must be known at compile time and cannot be dynamically resized without having to store the contents in a temporary variable when creating a new larger array, then copy all the data to a new one array and then clear the old array. With pointers, they do not behave in such a way, pointers can be dynamically allocated on the heap with the help of new ones, but should also be deleted if this variable is no longer used to prevent memory leaks, if the pointer is deleted before hand and something is trying to get to it access this memory address is no longer valid and does not belong to the caller, it is usually considered as unhandled exceptions, a lot of corruption, etc. and cause your program to crash. The same goes for arrays when you try to index them outside.
#include <iostream> int main() { // Arrays Are 0 Indexed int iArray[3] = { 1, 2, 3 }; // If you try to do this to access the last element std::cout << iArray[3] << std::endl; // Program Will Crash // Since Arrays Are 0 Indexed And The Size Of The Array Is 3 // The 3rd Value Is Indexed At 2, And By Trying To Index Location Of Array // At 3, This Memory Doesn't Belong To You, And It Is Undefined Behavior. // This Memory Could Contain Nothing, Random Data, Or Even Data That Belongs To // Something Else Which If Changed Can Even Cause System Failure return 0; } The first line should not compile, but there is a GCC error that forces it to compile and behave equivalently to something like
std::string city1[] = {"toronto", "toronto", "toronto", "toronto", "toronto", "toronto", "toronto", "toronto"}; (8 because "toronto" is 8 characters, including a trailing zero. Yes, that means if you used "Chargoggagoggmanchauggagoggchaubunagungamaugg" , it will create an array of 46 lines, each of which stores "Chargoggagoggmanchauggagoggchaubunagungamaugg" .)
Needless to say, you should not rely on a compiler error.
In case of a GCC error, city1 will be an array of std::string s; there is no operator << overload that supports printing such a thing. Instead, in std::cout << city1 array splits into a pointer to its first element, and instead the address stored in the pointer is printed.
You probably wanted to write std::string city1 = "toronto"; . One row, not an array.