Thread: string assignment

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    string assignment

    Code:
    for (i = 0; source[i] != '\0'; i++)
        destination[i] = source[i];
    based on this code, will the null termination byte be assigned to the destination? If yes why?

    Secondly, can i just simply use this

    Code:
    destination = source
    instead of the above code?


    and what about
    Code:
    *destination++ = *source++
    ?

    and what about this
    Code:
    *destination = *source
    ?

  2. #2
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    all work expected the following:

    *destination++ = *source++

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Not all of my questions are answered.. Please

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    >> based on this code, will the null termination byte be assigned to the destination? If yes why?
    No.

    Some of them might work depending on the rest of the code. As it's written, none of them work.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sounds like it might be homework.

    Why not try them and find out?

    The first one is the correct way to copy a string (although you'll have to manually add the '\0' to the end, I'll let you figure out why).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    Sounds like it might be homework.

    Why not try them and find out?

    The first one is the correct way to copy a string (although you'll have to manually add the '\0' to the end, I'll let you figure out why).
    For heaven's sake, this is not my homework of any kind. I am having doubt on it.

    And please.. Please quote because i do not know which first one you are talking about.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by valthyx View Post
    Code:
    for (i = 0; source[i] != '\0'; i++)
        destination[i] = source[i];
    That's the first one where you have to manually copy the '\0' into destination[i] after the loop.

    Quote Originally Posted by valthyx View Post
    Code:
    destination = source
    Yes, that works as well, but only if destination is declared as "char *". If you declare destination as an array of char, you can't assign a new starting address to this array.

    Quote Originally Posted by valthyx View Post
    Code:
    *destination++ = *source++
    No, that doesn't work, because the postfix-expression isn't a lvalue expression, so that you can't assign a value to it. For reference, please read the ANSI C standard ;-)

    Quote Originally Posted by valthyx View Post
    Code:
    *destination = *source
    ?
    yes, that should work as well, but you need then to manually increment the pointers, e.g. destination++;

    - Andi -

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by valthyx View Post
    Code:
    for (i = 0; source[i] != '\0'; i++)
        destination[i] = source[i];
    based on this code, will the null termination byte be assigned to the destination? If yes why?

    Secondly, can i just simply use this

    Code:
    destination = source
    instead of the above code?
    With the var names 'source' and 'destination' I'm assuming you want a copy of the string, so no you can't use the second example. The first code example copies the string, the second just copies the pointer to the string.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by ForzaItalia2006 View Post
    No, that doesn't work, because the postfix-expression isn't a lvalue expression, so that you can't assign a value to it. For reference, please read the ANSI C standard ;-)
    What? No, this should work.
    It's a common way of copying strings.
    This is a typical implementation of strcpy:
    Code:
    while (*src)
        *src++ = *dst++;
    Or even:
    Code:
    while (*src++ = *dst++) ;
    (Incomplete, however.)
    Last edited by Elysia; 01-26-2010 at 11:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ForzaItalia2006
    No, that doesn't work, because the postfix-expression isn't a lvalue expression, so that you can't assign a value to it. For reference, please read the ANSI C standard ;-)
    To explain Elysia's example: it is certainly true that the result of destination++ is not an lvalue. However, the result of *destination is a modifiable lvalue. Since the result of *destination++ is the result of *destination, the result of *destination++ is a modifiable lvalue, hence contrary to your assertion, the statement is valid C.

    Unfortunately, Elysia mixed up the src and dst , and I would argue that the fun canonical implementation of strcpy is:
    Code:
    while (*destination++ = *source++);
    (Of course, it is also incomplete since it is missing stuff like the return statement, but it is more complete than Elysia's example: even if src and dest were correctly swapped, one would need to append a null character after the loop.)

    EDIT:
    Oh wait, Elysia did list this as the second example.
    Last edited by laserlight; 01-26-2010 at 11:24 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Didn't say it was right
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by Elysia View Post
    What? No, this should work.
    It's a common way of copying strings.
    Yes, you're indeed right. I originally thought this should work (especially when working with pointers), but I got a compiler error that an lvalue is required. But I must admit, that I was to fast evaluating the error message. The error message was just saying, that an lvalue is required for the increment operand which was a char array instead of a char pointer.

    Though, sorry for the confusion :-)

    - Andi -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incompatible string assignment types
    By Tigers! in forum C Programming
    Replies: 9
    Last Post: 10-28-2009, 11:08 PM
  2. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  3. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM