3

can't figure out why i got this output from this code.

#include<stdio.h>
#include<string.h>
int main()
{
    char str[] = "I live in NY city";

    printf("%s%s\n","The string in array str[] before invokihg the function memmove(): ",str);

    printf("%s%s\n","The string in array str[] before invokihg the function memmove(): ",memmove(str,&str[7],10));
        return 0;
}

My output is : in NY cityNY city
isn't that should be : in NY city i live ??

Here is similar example from my book and it make sense tho.

#include<stdio.h>
#include<string.h>
int main()
{
    char x[] = "Home Sweet Home";

    printf("%s%s\n","The string in array x[] before invoking the function memmove(): ",x);

    printf("%s%s\n","The string in array x[] before invoking the function memmove(): ",memmove(x,&x[5],10));
        return 0;
}   

And the output here is: Sweet Home Home
Which is right according to definition of memmove() function, that is, copying a specified number of bytes from the object pointed to by its second argument into the object pointed to by its first argument.(withing the same string) Also object here refers to a block of data.

Stanley
  • 394
  • 1
  • 13
  • 1
    "isn't that should be : in NY city i live ??"" No. How would the part "I live" be moved to the end of the string? – Gerhardh Aug 06 '18 at 12:52
  • I think the example in your book isn't very good, as you can't tell that the first "Home" has disappeared and the second "Home" has been duplicated. – molbdnilo Aug 06 '18 at 14:22
  • @molbdnilo yep, the definition isn’t that straightforward and confusing example made me understood wrong this function. Anyway it’s C How To program 4th edition by H.M.Deitel – Stanley Aug 06 '18 at 14:27
  • Speaking of books, u guys could suggest some for C++ ? Not for beginners and not for advanced programmers neither. Smth between, with more examples and practical exercises. The keyword for my request is PRACTICE, GOOD PRACTICAL EXAMPLES, PLAIN ENGLISH. – Stanley Aug 06 '18 at 14:35
  • @Stanley The "semi-official" stackoverflow C++ book recommendations are [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Aug 06 '18 at 14:39
  • @molbdnilo appreciate it mate – Stanley Aug 06 '18 at 14:42

2 Answers2

9

Your expectations are wrong.

You define a string:

char str[] = "I live in NY city";

Then you move (copy) 10 bytes from the end to the beginning of the string:

"I live in NY city\0";
 012345678901234567
       /        /
      /        /
     /        /
    /        /
   /        /
  /        /
"in NY cityNY city\0";

Everything else is not touched.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
4

I think you're under a wrong impression that memmove is supposed to somehow swap the non-overlapping parts of the destination and the source. This is not how it works. You're probably confusing the example from your book because the word "Home" appears twice. Change it to "Home Sweet HOME" so you can see what's going on, and then read the documentation/specification of memmove rather than guessing at what it does.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711