Results 1 to 4 of 4

Thread: C oddity?

  1. #1
    Aaron_Adams
    Guest

    C oddity?

    I don't understand the following.

    If I were writing a program equivelent to strcat for instance:

    Code:
    void strcat(char *s1, char *s2)
    {
        while(*s1++);
        while(*s1++ = *s2++);
    }
    The above, IMHO should work. Yet it does not. Although *s1++ = *s2++ works fine for the second while loop, *s1++ for the first while loop does not. You must instead write:

    Code:
    void strcat(char *s1, char *s2)
    {
        while(*s1)
            *s1++;
        while(*s1++ = *s2++);
    }
    Can anyone explain to me why? I understand it really makes no speed difference or anything, I'm just boggled as to why it doesn't work.


  2. #2
    Aaron_Adams
    Guest

    Re:C oddity?

    If anyone is interested in the solution to this, there was a respone on linuxnewbie.org

    The solution is
    Code:
    void strcat(*char s1, *char s2)
    {
        while(*++s1);
        while(*s1++ = *s2++);
    }
    The reason being is because the first while statement was testing for '/0', would find it but still increment past it, thus screwing up the testing of the next while statement. From what I understand anyways.

  3. #3

    Re:C oddity?

    :'(

    Had I seen this post I could have answered it. I hate pointers, I think I have had every friggin problem in the world with pointers, this being one of them. :-\

    Pointers are a pain in the arse!

    Aragorn
    If you give a man a fire he'll be warm, if you light the man on fire he'll be warm for life.

  4. #4
    JimH
    Guest

    Re:C oddity?

    Hmmm, I seem to have somehow missed this thread too. :P

    Jim H

Similar Threads

  1. Laptop Oddity
    By Aaron_Adams in forum Linux - General Topics
    Replies: 2
    Last Post: 07-03-2002, 04:17 PM
  2. Mozilla oddity
    By ASCI_Blue in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 02-24-2002, 09:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •