Thread: Simple String Copy Question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Simple String Copy Question

    Hi guys,

    Okay, I'm a 2nd year computer science student and I'm taking my first C++ course. I have some background in program and have used C++ for graphics programming but I'm reading the first few pages of my profs notes and am already stumped!

    Here's the code snippet that's causing me the distress:

    Code:
    int i = 0;
    do
    {
    s[i] = t[i];
    i = i + 1;
    } while (s[i] != '\0');
    The purpose of this code is to copy a string. I understand that \0 is an indicator of the end of a string in C++ but the condition of the while loop is check after incrementing i. So, from what I can tell, isn't s[i] referring to a still untouched index of the array?

    Thanks,

    Canadian0469

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    int i = 0;
    do
    {
      s[i] = t[i];
      i = i + 1;
    } while (s[i] != '\0');
    Means

    Starting with a number i who has the value of 0
    Copy the i element of the array "t" to the i element of the array "s"
    Increment i by one
    Until the i element of "s" is equal to zero.

    Do I see any issues with this code? Sure, what if t is not large enough to hold all of the s values inside of it? And no, keep in mind that your code will do all of that stuff then execute the while condition and check to see what is going on. A loop designed like this even copies over the '\0' (null terminator).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yuk!
    Looks a little too dangerous for my taste.
    I'd probably do something more like this:
    Code:
    int i = 0;
    do
    {
        s[i] = t[i];
    } while ( s[i++] != '\0' );
    "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

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which still assumes a lot of "t." What specifically is the assignment here, Canadian0469?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That code assumes that s is not an empty string (among other things). There's no reason to assume that when cpjust's version is equally effective and doesn't make the assumption.

    It also assumes that t is at least as large as s, which is probably acceptable for the exercise.

    >> I understand that \0 is an indicator of the end of a string in C++ <<

    Actually, it indicates the end of a string in C. It's just that you can also use C style strings in C++ and some out-of-date books/tutorials/instructors still teach them. (Note that in this case the point of the code is to look at the algorithm, so it's not a problem to be using C style strings for this exercise).

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Alright I'm not looking at alternative ways to write this piece of code, I'm only trying to understand the example.

    Let me explain what is confusing me. I understand that it's trying to copy the string stored in array t to array s. Look at how I would trace the example where array t holds the string "a\0", i.e. t[0]='a' and t[1]='\0':

    At first we have:
    i=0
    s[0]=0
    s[1]=0
    t[0]='a'
    t[1]=\0

    Then, first iteration of loop:
    s[0]='a'
    s[1]=0
    i=1
    checks is s[1] != '\0'? Yes, it's still uninitialized so continue...

    Then, second iteration of loop:
    s[1]=\0
    i=2
    checks is s[2] != '\0'? Yes, it's still unitialized so continue...

    This is the part where I get confused. I thought at this point it should be seeing that s[1] has been changed to \0, instead, because i was incremented in the loop, it doesn't even see this change as its always checking the index of the array that is 1 index following the one that just got changed... Am I wrong?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    It also assumes that t is at least as large as s, which is probably acceptable for the exercise.
    Don't you mean that s is at least as big as t?

    The way the teacher wrote it, the size of t would need to be at least strlen( t ) + 2, since it's always copying 1 past the NUL terminator; otherwise it could be accessing memory on a new page boundary that it doesn't own. And obviously s would also need to be at least that size.

    EDIT: Oh and there's another bug. Since the teacher's while loop increments i and then checks if s[i] != '\0', what if your s string starts out as "a\0" and t is "xyz\0"? After copying x to s[0], it will check s[1] and see that it's already '\0' and not bother copying the rest of t.
    Maybe you should tell your teacher about those bugs in the code.
    Last edited by cpjust; 09-17-2008 at 08:21 PM.
    "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

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Canadian0469 View Post
    This is the part where I get confused. I thought at this point it should be seeing that s[1] has been changed to \0, instead, because i was incremented in the loop, it doesn't even see this change as its always checking the index of the array that is 1 index following the one that just got changed... Am I wrong?
    Yup, that's the big problem with that code.
    "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

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    What a great way to start the semester... That was seriously my first example.

    Anyway, thanks for the help guys! I just wanted to make sure I was tracing it correctly and that it was actually an error.

    Cheers!

    Canadian0469

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Canadian0469 View Post
    What a great way to start the semester... That was seriously my first example.

    Anyway, thanks for the help guys! I just wanted to make sure I was tracing it correctly and that it was actually an error.

    Cheers!

    Canadian0469
    Hopefully the teacher isn't doing anything else dumb like void main()...
    "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

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Don't you mean that s is at least as big as t?
    Yes, I missed the most important thing - that s was the target of the copy and t was the source.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    that s was the target of the copy and t was the source.
    Oh dear!

    I do hope the example was given to you as an error search example, and not something serious. There's so much wrong with it ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple String Manipulation (Beginner question)
    By valdro in forum C Programming
    Replies: 4
    Last Post: 02-16-2009, 07:16 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM