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

How to use the same multidimensional array in many functions?
C++ namespace: using without owning
How can I change the border style of a Tab Control?
gtest installed with conan: undefined reference to `testing::internal::GetBoolAssertionFailureMessage`
how much memory would it take to store a reference to an object in C++?
Per vertex weighted normal keeping in mind the crease angle in OpenGL C++
std::vector< T >::iterator with accessible constructor
Permanently storing an environment variable when using CMake install
Calling derived class methods using array of base class pointers which has pointers to derived class objects
error LNK2019: unresolved external symbol Direct Sound
c++ passing functions with variable number of arguments as argument to other function
Xcode won't compile due to LLVM error
save total number of frames of video file into a .txt file
Mix_Halt specific tracks?
Why I cannot use boost correctly with CMake?
OPEN CV c++ person Recognition K-nn

Categories

HOME
node.js
gnuplot
git
loops
sitecore8
selenium-webdriver
composer-php
veins
informix
formula
rectangles
pivot-table
bpm
astyle
mit-scratch
corba
httpresponse
angularjs-ng-route
subplot
react-boilerplate
.net-4.5
unimrcp
google-developers-console
coap
navigation-drawer
edi
filenames
ireport
maxlength
jquery-ui-autocomplete
modeling
live
dos
tridion
webdrivermanager-java
envelope
uievent
yield
jquery-ui-widget-factory
matlab-uitable
object-oriented-analysis
django-compressor
django-media
android-build
ucos
docker-registry
prime-factoring
synopsys-vcs
rollbar
google-chrome-arc
stdio
news-feed
sharepoint-userprofile
dde
compass
nested-attributes
page-load-time
firefox-addon-bootstrap
sonarqube-5.4
gridster
rebase
datamapper
mindbody
eclipse-emf-ecore
dynamics-nav-2009
apache-commons
eofexception
xlet
fluid-layout
protection
android-view
build-system
image-manipulation
iec61131-3
google-plugin-eclipse
sql-job
filefield
unassigned-variable
ember-qunit
lcm
stop-words
kooboo
bonjour
graphael
resharper-7.1
phpmotion
presentmodalviewcontrolle
spn
inner-classes
divshot
tandem
wexitstatus
trusted-timestamp
launching-application
mobile-phones
in-app
wind-river-workbench
visualtreehelper
juggernaut
rebuild
cck
olpc

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