Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

using strcpy in c++ error

Posted on 2011-03-21
7
394 Views
Last Modified: 2012-08-13
Can you please tell me what's wrong with this code?
I need to use strcpy for my homework and I keep getting a "Run-time check failure #2:Stack around st2 was corrupted"

btw, I am trying to just print State

Thanks guys!
#include<iostream>
#include<cstring>

using namespace std;

int main(){
	char st1[20]="State of Ca";
	char st2[5]={' '};

	strcpy(st2, st1);
	cout<<st2<<endl;
	
return 0;
}

Open in new window

0
Comment
Question by:pgmerLA
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 300 total points
ID: 35185946
st2 is only 5 elements in size, which is too small to fit all of st1 in it. If you only want to copy part of the string, then use strncpy, where the third argument is the no. of char's to copy. Eg:

const int ST2_LEN = 5;
...
char st2[ST2_Len] = {' '};
strncpy(st2, st1, ST2_LEN);
...

Open in new window

0
 

Author Comment

by:pgmerLA
ID: 35185954
is there any way I could use strcpy?
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35185967
strcpy always copies the whole of the source (st1), into the destination (st2), as such the destination must be at least as large as the source, including null-terminating character.

So, to copy only "State", or have the destination string smaller than the source, no.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 200 total points
ID: 35186107
BTW, if you want to store "state" as a c-style string in an array, then don't forget that you need a terminating null byte for a total of 6 bytes. Your st2 is only 5 bytes.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35186111
oops, crysallus, I didn't see that you said "including null-terminating character". So, never mind.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35186181
Actually, you were still kind of right to pick up on that, as I hadn't put 6 in my sample code in my first post, which I should have.

But yes, my explanation there probably should have got that across.
0
 

Author Comment

by:pgmerLA
ID: 35186453
Thanks guys.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 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