How can no. of characters greater than its size? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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??

28th Jan 2021, 7:01 AM
Illa Bahadur Darlami Magar
Illa Bahadur Darlami Magar - avatar
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.
28th Jan 2021, 7:28 AM
Arsenic
Arsenic - avatar
+ 3
name only hold a pointer to the first string character...
28th Jan 2021, 7:03 AM
visph
visph - avatar