[help] i have set the size of character array 2 byte then how can it store more than 2 byte??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[help] i have set the size of character array 2 byte then how can it store more than 2 byte???

i am using visual studio 2017 code : #include<iostream> using namespace std; int main() { char a[2]; cout << "enter the name" << endl; cin>>a; cout << "the name is " << a << endl; cout << endl; system("pause"); return 0; } output: enter the name class the name is class

6th Nov 2019, 7:52 AM
Sami
Sami - avatar
2 Answers
+ 4
C++ don't check for array bounds. You can write in memory which is beyond limit of that array but you should not do that. It can cause loss of some important data by overwriting memory. Better use a std::string for this purpose.
6th Nov 2019, 8:15 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
Allocate more bytes than the name might use. Use try and catch to deal with exceptions.
6th Nov 2019, 8:13 AM
Fernando Pozzetti
Fernando Pozzetti - avatar