Thread: damn strings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Sebastiani
    >You attached "t" to "target" but then increment "target".
    Yes, but it doesn't matter which you do. You could just as easily put these the other way round, like you imply. You need to keep a pointer to the original target so you can return it at the end of the function.

    >You also try to increment "source". You can't do that. Run the function and see for yourself. The compiler treats the char* parameters as if they were the names of arrays, an the copy will fail.
    The parameters are treated exactly as defined, pointers to chars. This means pointer arithmatic works fine. Here's an example of strcpy() from K&R2 to prove it.
    Code:
    void strcpy(char *s, char *t)
    {
        while (*s++ = *t++)
            ;
    }
    >When max = 0, the chance to guarantee that null term is lost...so your loop should be
    >while(max > 0)
    max>0 is tested (well, kind of) in my loop. I can't be bothered to research this too far, what I wrote compiled/ran as expected (or so I found).

    >Finally, your point that it is the callers responsibility to check for NULL I have one argument: if perchance "source" is NOT null termed, then checking for a non-NULL pointer is a last-ditch attempt to end the copy loop, which may otherwise run infinitely...
    These are two different things.. am I understanding you here? The pointer being NULL will result in my function crashing. This is why the caller should ensure it's a good pointer. The null term of the string should always be there, else we'll probably go onto memory that's not ours. I think we're saying the same thing

    >Not to sound nit-picky, but...
    Please do, it's better that way
    Last edited by Hammer; 06-22-2002 at 05:52 AM.
    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. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM