Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using strcpy in c++ error

Posted on 2011-03-21
7
Medium Priority
?
406 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 1200 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
Industry Leaders: 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!

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

824 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