Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using strcpy in c++ error

Posted on 2011-03-21
7
Medium Priority
?
403 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

670 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