Creating char array using new operator!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Creating char array using new operator!!!

Cin>>*p; Takes only one character even though i have mentioned char *p=new char[20] Why??? Hasn't the OS allocated 20 bytes for p now so why my program crashes when i enter a multiword character even if i have declared the array for it?

9th Oct 2018, 5:35 AM
Bunny
Bunny - avatar
3 Answers
+ 1
There is a little difference between p* and a[20]. In the last case type contains size of array. cout (not a cin) is able to output arrays (usually string constants like "value"), but not a clear pointer. Use std::string to handle strings.
9th Oct 2018, 6:07 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 1
Additionally, to get input you use cin >> <variable>; instead of cin << <variable>; I'll take it as a typo, but a good point to remember, still.
9th Oct 2018, 8:05 AM
Ipang
+ 1
Yes ipang i messed a little
9th Oct 2018, 3:17 PM
Bunny
Bunny - avatar