Solved

using strcpy in c++ error

Posted on 2011-03-21
7
396 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
[X]
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
  • 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
Independent Software Vendors: 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 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

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

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 107
Eclipse IDE - Cannot copy/paste from console output 8 321
What is sub-make ? 2 88
max float value 3 59
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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