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

Linker errors when optimization greater than -O1 (clang++)
C++ returning MyClass* vs MyClass
Using part of enum as array index
Setting up my ATL COM callback functions
Is it correct to assume that diamond inheritance breaks encapsulation in C++?
Iterate over vector of vectors with one iterator
How to reference on deployment VS 2015 C++ Linker library dependencies?
How to receive multicast packets from already subscribed group?
C++ how to read __int16_t integer using scanf function
Is it possible to write and read from a text file with two programs simultaneously
TCP Socket - Server not accepting any connections UNIX
How I can determine physical RAM installed on computer? (windows)
Installing Clipp Library into Visual Studio 2015
Converting char* to str strange output
What is the difference between a modifiable rvalue and a const rvalue?
Why does “template argument deduction for class templates” not work on a plain struct?

Categories

HOME
node.js
slickgrid
selenium-webdriver
graphite
composer-php
netlogo
conditional
mutex
contiki
visual-foxpro
playframework-2.5
widget
stormcrawler
face-recognition
google-play-games
m3u8
angularjs-ng-route
jquery-gmap3
riscv
4d-database
polymer-1.0
file-permissions
virtuoso
jsx
webcam
autoload
jointjs
master-detail
drools-fusion
uisegmentedcontrol
pam
sqlcmd
bspline
reddit
javassist
release-management
implicit
siteminder
qt4.8
cscore
ch
sequencing
imshow
mechanize
smf
google-plus-domains
nokia
rouge
yahoo-messenger
ionic-view
oracle-streams
http-accept-header
pagerslidingtabstrip
acl2
unexpectendoffile
soundex
custom-validators
scrollviewer
android-view
srv
review
google-plugin-eclipse
data-sharing
raii
pyro
sandbox-solution
offlineapps
filtered-lookup
jquery-ui-selectable
windmill
appmobi
ctime
32feet
glossary
plcrashreporter
eda
paint.net
orbited

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile