Variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Variable

Hello, While coding you have noticed that if you select float or int data type for string data.. Error says, Can't convert to float from 'const char*' And this two words also help you to create a string type variable.. Now the question is, Does, c++ counts strings as an constant array of characters?? https://code.sololearn.com/crbf4UI58Wd3/?ref=app Another thing const char* - - - 8bits string - - - 32bits Why??

13th Apr 2021, 12:51 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
2 Answers
+ 1
C++ string can not be seen as a character array because the quality of changeable length of characters. As we all know array is fixed sized. std::string works like array because you access the character like you do in array, but in reality it's not an array. About sizeof, An array is a type that allocate a continuous memory to store the elements of the same data type, while std::string is a class and rather complicated. We do not really know how std::string is implemented, but since it's a class, we can guess that it has other various members and functionality which potentially increase the size of a string.
13th Apr 2021, 2:12 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Roger that
13th Apr 2021, 5:17 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar