+ 4

How is the output 'a'

I was asked this question in Solo learn Char *ptr ="abcde"; Count<<*ptr++; Ok I guess I haven't learn this yet

14th May 2019, 5:07 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
6 Answers
+ 6
By the way, it is cout<<*ptr++; And that is because the pointed address is defaulted at array index 0, so when post increment is applied, you print the object in the current address before moving on to the next address.
14th May 2019, 5:14 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
Ptr is pointing to the 'a' character in the string and that's what should get printed. The increment to the pointer happens later as it is a post increment. BTW it's cout not Count.
14th May 2019, 5:12 AM
Sonic
Sonic - avatar
+ 3
Because pointer is store the address of first index of the array or string.and increment operator (++) increase the address of the pointer and pointer point to next index of the array or string.
14th May 2019, 6:02 AM
Er. Mohd. Shadab
Er. Mohd. Shadab - avatar
+ 2
Yeah may be I thought syntax for declaration of pointer is something like this data type *name
14th May 2019, 5:14 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
0
Pointer allways points on first value in a list if u do cout<<*++ptr; it will show 'b' but when u do cout<<*ptr++; it shows 'a' you can also follow code with cout<<*ptr; on the next line, and it will print 'b'
14th May 2019, 9:12 PM
Show Man
Show Man - avatar
0
Becauae of the pointer is post increment it will print the first element in the array i.e., ' a ' and then incremented by 1 then it will print 'b' and so on.....
15th May 2019, 8:26 AM
Nandyala Priyanka
Nandyala Priyanka - avatar