1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C programming error

Discussion in 'Meet and Greet' started by CrazyGal, May 29, 2009.

  1. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hi there,

    Could someone tell me why this wont work?

    #include <stdio.h>
    int main()
    {
    char p[10] = "Success";
    char *t = p;
    while (*t++ != '\0'){
    printf("%c ", *t);
    *t = 'i';
    }
    return 0;
    }

    If I move the increment part from while to the assignment statement, it works fine.
    ie., *t++ = 'i'; It prints Success. The code above doesn't print anything.

    Let me know if I'm missing something here.

    Thanks a lot.
    Tina
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Seems like this is your first post.
    So, welcome to the forum :clap::clap:

    Now, about your code ... hmmm

    First of all, using while(*t++ != '\0'){ is bad because it will NEVER print the first letter as t is incremented immediately.

    Next, I don't think your code will print NOTHING. Which compiler do you use ? I think it will print "u c c e s s" and then some garbage. On my machine it shows :

    u c c e s s " ╥ ‼ @ l ‼ @ ≡ ² ⌂ ░ * " K ↕ @ ☺ H ? > └
    , > @ @ ñ * " * * * * ¿ * " æ î O Ç └ , > ≡ ² ⌂ └
    * " ÿ ↕ @ ☺ ≡ * " g p ü | Ö x ┤ ≈ " ≡ ² ⌂ ╕ ╢ T Ç ╚
    * " ¿ ═ ╥ ü * * * * └ Ü â | p p ü | Ç ↕ @ A



    Now why does this happen ?

    Let's analyze step-by-step :
    Loop 1 : Step 1 : *t is checked for '\0' and returns false as *t = 'S'.
    .............Step 2 : t is incremented and points to 2nd char = 'u'.
    .............Step 3 : *t is printed with a space after it.
    .............Step 3 : *t is changed to 'i'.

    Loop 2 : Step 1 : *t is checked for '\0' and returns false as *t = 'i' (look at last step of Loop1).
    .............Step 2 : t is incremented and points to 3nd char = 'c'.
    .............Step 3 : *t is printed with a space after it.
    .............Step 3 : *t is changed to 'i'.

    Loop 3 : Step 1 : *t is checked for '\0' and returns false as *t = 'i' (look at last step of Loop1).
    .............Step 2 : t is incremented and points to 4th char = 'c'.
    .............Step 3 : *t is printed with a space after it.
    .............Step 3 : *t is changed to 'i'.

    Do you see what is going on ?
    Whenever *t is checked for '\0', it contains 'i'. So, the program falls into an infinite loop and terminates when an exception occurs.

    t should be incremented after changing *t to 'i'.
    But you, increment t first and then change it to 'i'. So, it's useless !

    Hope this will help.
    Best of luck.
     
  3. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanks for responding!
    I see the error now.

    I got confused with the strcpy version :
    void strcpy(char *s, char *t)
    {
    while(*s++ = *t++);
    }

    So, the point is when you've increment in while loop, then you shouldn't use that
    operand in the function body.

    Btw, I used gcc compiler and I dint see any print on the screen.

    Thanks again,
    Tina
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure ! :biggrin:

    [ I use gcc (MinGW) too ! ]
     
  5. it career

    it career New Member

    Joined:
    Apr 8, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Designing
    Home Page:
    Put additional bracket
    while(*(s++) = *(t++));
    Now it should be clear.
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
  7. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Ummm....Doesn't this fetch the character first and then increment? Like (*s)++. If it increments the pointer first, this version of strcpy wont work. Is that right?

    Tina
     
  8. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    I don't see any reason why that version should not work ! :surprised:
    It works fine on my machine.
    Adding brackets, as suggested by it_career is unnecessary as the compiler automatically interprets *s++ as *(s++).

    Look, I think you don't get it properly.

    Observe at the loop : while(*s++ = *t++);
    What this does is :
    (1) Assign *t to *s.
    (2) Increase t and increase s.
    (3) As nothing to do inside loop body, so go to step (1).

    It does not increment the pointer first ! It's post-fix ++.

    In the case you mentioned in post #1, first the while condition was executed, then t was increased and then the body was executed. So, the first char was missed out.
    But here, nothing is done in the loop-body.

    And one more thing, never do anything as crazy as (*s)++ because that will change your string by increasing each character by 1.

    Hope I made it clear to you.

    Best of luck.
     
  9. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Umm..My mistake again...That's what I meant but I got confused with the brackets. Its crystal clear now :)

    Thanks!
    Tina
     
  10. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure ! :biggrin:
     

Share This Page