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

Default for ADTF filter trigger
Why the result of assigning an out-of-range value to an object of signed type is undefined in C++? [duplicate]
How to detect symmetries in 4 integer variables efficiently?
C++ Pangram Program
Horspool algorithm execution error
What does this code do in ns2?
cmake c++11 flag not included when makefile is generated
Why should I input on the second line of the console?
C++ Inheritance - Overriding Function, including using “::”s, .h files & .cpp files
Optimizing the grammar [Resolved]
Duplicating stdout [duplicate]
error: ld returned 1 exit status I Build failed: 2 errors
boost/asio async_read() TCP Server - Understanding io_service.run() on linux
Size of encoded avro message without encoding it
variable not getting passed between classes? are my for loops structured wrong?
audio programming SDL - How to specify the cut of frequency in command line with wav file

Categories

HOME
cordova
coldfusion
cron
firebase-authentication
luigi
cakephp-3.0
ipad
inform7
distributed-database
crystal-reports-xi
cognos-10
eventtrigger
odbc
multiline
xlwings
qtspim
summary
deprecation-warning
miktex
oracle-data-integrator
avaya
git-clone
provider
geckofx
affiliate
week-number
wasapi
atom-feed
phpexcel-1.8.0
gperftools
uwamp
crawler4j
tornado-motor
cassandra-3.0
card-flip
watchdog
laravel-excel
yield
clisp
mosync
appharbor
gitversion
strconv
wso2dss
angularjs-service
libtool
cscope
erwin
use-case
banking
turtle-rdf
twebbrowser
git-stash
jodconverter
virtual-directory
codeigniter-restserver
grid-layout
gethostbyname
google-roads-api
anonymity
xcode4
iterm
pdf-extraction
datamapper
swift-mt
cirqus
django-pipeline
excel-2008
nstablecolumn
android-bundle
zipcode
ncbi
visual-studio-2010-sp1
seeding
code-completion
irssi
filefield
mooc
unassigned-variable
javascript-databinding
xcode6.1
xcode-instruments
code-metrics
textile
wxgrid
android-x86
ubuntu-13.04
icommand
amber-smalltalk
jwebunit
ios5.1
operational-transform
nservicebus3
keymando
in-app
flotr
incremental-linking
projective-geometry
open-basedir
flashlite

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile