c++


Copying a string char by char in another string


I have a string type and I am trying to copy this into another string type char by char but when I try to display the output of new copied it is coming as blank. Where as when I try to output char by char for that copied string length its alright. Please see this small code and output for better understanding.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Tom";
string temp;
int i = 0;
for(; i < name.length(); ++i)
{
temp[i] = name[i];
}
cout<<name<<endl; //gives the output Tom
cout<<temp<<endl; //gives blank
for(int i = 0; i < name.length(); ++i)
{
cout<<temp[i]; //gives output char by char
}
}
Output :
Tom
Tom
I know there are other possible ways to achieve what I am trying to do but just out of thought tried this and it didn't work. So looking for some explanation.
Note that temp is empty, its length is 0. Then temp[i] = name[i]; leads to undifined behavior; anything is possible but nothing is guaranteed.
You can give it an initial length like:
string name = "Tom";
string temp(name.length(), '\0');
You have to allocate data for the temp string to change any data in it.
string temp(name.length(),'\0');

Related Links

Making program to close automatically after clicking on NO on a MessageBox (C++)
opengl32.lib wont link to project
Condition checking: if(x==0) vs. if(!x)
How to show list of strings in QT at run time?
Passing struct to function in C++ [closed]
Why is this swap function call ambiguous? [duplicate]
C++ Storing rows as part of a 2D array and then printing a row [closed]
Segfault: Track down “invalid read” “free'd at”
Comparing 2 text files
Assigning a string to an element of an array?
Memory allocation and assignment in C++ with openMP
Change QComboBox item text in delegate
FFmpeg av_read_frame returns a size but no data?
Unusual variable declaration
mfc c++ update text file from edit control
Passing attributes to OpenGL vertex shader acts strangely

Categories

HOME
firebase-authentication
text
authorization
custom-attributes
freeswitch
google-places-api
mutex
jaxb
rectangles
jestjs
dynamics-ax-2012
stormcrawler
batch-normalization
sirikit
divide-and-conquer
code-injection
swtbot
robocopy
pfsense
apl
github-api
chinese-locale
pi4j
coupon
modeling
hp-quality-center
podcast
chmod
bluehost
aircrack-ng
x509
gulp-4
lsmeans
transfer-function
state
netbanx-api
cassandra-3.0
psftp
uicollectionviewcell
web.py
shieldui
fixture
retrieve-and-rank
paypal-adaptive-payments
exceljs
orika
javassist
sqlcommand
jquery-tags-input
python-wheel
datepart
testdroid
zend-studio
network-monitoring
dsquery
gridster
cirqus
sqlsrv
template-strings
mpi4py
acronym
order-of-operations
ncbi
voog
mashape
lightroom
magnetometer
zenoss
file-attributes
image-manipulation
mysql-error-1093
mooc
string-search
teamsite
restriction
objectarx
google-playground
styledtext
gideros
ios6.1
mdbtools
jquery-ui-selectable
customer-support
incompatibletypeerror
nspasteboard
google-doodle
assetslibrary
statusnet
tao
rebuild
xml-comments

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App