Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   c++ doubt (http://www.go4expert.com/forums/cpp-doubt-t26903/)

dreskei 11Oct2011 02:10

c++ doubt
 
char *cstr1="hello";
char s[]="tick"; cstr1=s;
for(y=0;y<4;y++)
{
cout<<" "<<*(cstr1+y)<<" "<<(*cstr1)++;
}

OUTPUT: u t i u c v k w

and if it is
char *cstr1="hello";
char s[]="tick"; cstr1=s;
for(y=0;y<4;y++)
{
cout<<" "<<*(cstr1+y);
}

OUTPUT IS : t i c k

how can dis be possible plz somone help

xpi0t0s 12Oct2011 02:50

Re: c++ doubt
 
The problem is in the use of the post-increment operator. Don't use it until you are completely familiar with its use because it will confuse you...............exactly as it just has done.

sura 28Oct2011 23:00

Re: c++ doubt
 
in *(cstr1+y)
here the adress is incremented by the value of y (i.e 1,2,3,4) and the memory location in the adress is displayed
but in (*cstr1)++
here the adress is incremented by the value of y (i.e 1,1,1,1) and the memory location in the adress is displayed
so there is a change in adress location that why so confusing..................

dreskei 1Nov2011 03:47

Re: c++ doubt
 
Quote:

Originally Posted by xpi0t0s (Post 87865)
The problem is in the use of the post-increment operator. Don't use it until you are completely familiar with its use because it will confuse you...............exactly as it just has done.

hmm ok ll learn more abt it


All times are GMT +5.5. The time now is 08:00.