[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ strcpy

Posted on 2008-11-06
4
Medium Priority
?
946 Views
Last Modified: 2012-05-05
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
Comment
Question by:markdot
  • 2
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22901542
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
 

Author Comment

by:markdot
ID: 22901594
Then, how can we shift the sub-string "defg"  in char s[100]={'a','b','c','d','e','f','g'}?
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 360 total points
ID: 22902041
either use memmove or copy them in a loop char by char
0
 

Author Comment

by:markdot
ID: 22902123
That is what I need!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question