How to access the pointer to the character array?? What is the expected output ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to access the pointer to the character array?? What is the expected output ?

char *name = NULL; name=new char[5]; name[0]='G'; cout<<name; //If you think the output is G PLEASE can you explain me the whole working of it!

8th Nov 2019, 9:10 PM
Puneet Goel
3 Answers
+ 3
The indexing in array starts from 0. So the pointer will point to the first element of the array. And your first element that you have assigned is G. So answer is G.
8th Nov 2019, 9:33 PM
Avinesh
Avinesh - avatar
+ 2
When you assign an array to a pointer then always remember that the pointer will point to the first element of the array which is obviously name[0] in your case so G is printed.
8th Nov 2019, 9:28 PM
Avinesh
Avinesh - avatar
+ 1
So name[1] is correct ???
8th Nov 2019, 9:31 PM
Puneet Goel