why codes this work?

This is a discussion on why codes this work? within the C Programming forums, part of the General Programming Boards category; I wrote a string copy routine for learning purposes and then I dellibratly made it leave the

Thread: why codes this work?

off the ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    28

    why codes this work?

    I wrote a string copy routine for learning purposes and then I dellibratly made it leave the \0 off the destination string and it still works why is this?

    Code:
    char *chrstrcpy(char *des,char *src)
       {
           char *p = des;
           while (*src != '\0')
           {
               *p++ = *src++;
           }
    
           return des;
       }

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    it does not work for me. maybe you forgot to recompile your program

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to define what you mean by works and does work.
    Code:
    #include <stdio.h>
    
    char *chrstrcpy(char *des, char *src)
    {
        char    *p = des;
        while (*src != '\0')
        {
            *p++ = *src++;
        }
    
        return des;
    }
    
    int main(void)
    {
        char    s[10] = "aaaaaaaa";
        puts(s);
        chrstrcpy(s, "BBBB");
        puts(s);
        return 0;
    }
    
    /* Output
    
      aaaaaaaa
      BBBBaaaa
      
      */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    would the \0 clear the end of the old string?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Gav D
    would the \0 clear the end of the old string?
    Try it and find out.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    sorry I know it then terminate the string and the program wouldnt show the left over value form the old variable but would it still be in memory or would that be cleared?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>would it still be in memory or would that be cleared?
    Again, try it and find out. You'll need something like this:

    Code:
    for (i = 0; i < arraysize; i++)
    {
      putchar (array[i]);
    }
    Short answer though, nothing is "cleared" unless you specifically write over it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  5. C/C++ Code(s)
    By medhjai in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2005, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21