• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

C++ strcpy

void main()
{
      char s[100]={'a','b','c','d','e','f','g'};
      strcpy(s+4,s+3);
      cout<<s<<endl;
      return;
}
I thought it should be abcddefg, but it is abcdddfg. Can you explain in detail?
0
markdot
Asked:
markdot
  • 2
  • 2
1 Solution
 
sunnycoderCommented:
The behavior is undefined
http://www.opengroup.org/onlinepubs/000095399/functions/strcpy.html
If copying takes place between objects that overlap, the behavior is undefined.
0
 
markdotAuthor Commented:
Then, how can we shift the sub-string "defg"  in char s[100]={'a','b','c','d','e','f','g'}?
0
 
sunnycoderCommented:
either use memmove or copy them in a loop char by char
0
 
markdotAuthor Commented:
That is what I need!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now