+ 1
How can no. of characters greater than its size?
#include<iostream> #include<string> using namespace std; int main{ string name="Hello my name is abcdefghijklmnopqrstuvwxyz"; cout<<endl<<name.length(); cout<<endl<<sizeof(name); return 0; } Output is 43 32 theres 43 characters so logically size must be 44bytes but its size is 32 bytes how??
2 Answers
+ 6
"sizeof(name)" is not the size of the string it have inside it, instead is the size of object of class std::string.
similar to how sizeof(1234) will give size of "int" datatype and not the number you are giving.
+ 3
name only hold a pointer to the first string character...