c++ doubt

Discussion in 'C++' started by dreskei, Oct 10, 2011.

  1. dreskei

    dreskei New Member

    Joined:
    Oct 22, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    delhi
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. sura

    sura Banned

    Joined:
    Aug 4, 2011
    Messages:
    47
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    India,Tamil Nadu.
    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..................
     
  4. dreskei

    dreskei New Member

    Joined:
    Oct 22, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    delhi
    hmm ok ll learn more abt it
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice