473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

missing zeroterminator in string.c_str()?

In the following program I would expect r to be a zero terminated string of
length 1. Am I wrong? The STL I'm using omit the zeroterm in this situation.
Klaus

#include <iostream>

using namespace std; //introduces namespace std

int main(){
string x("12");
string::iterator p(x.begin()),q(x.begin()+1);
string y(p,q);
const char *r=y.c_str();
cout <<r << " "<< int(r[0])<< " "<< int(r[1]);
return 0;
end;
Jul 19 '05 #1
2 2687
"klaus hoffmann" <dr***********@t-online.de> wrote...
In the following program I would expect r to be a zero terminated string of length 1. Am I wrong?
The program is not supposed to compile. 'string' is undefined.
Once you #include <string>, it works fine for me. 'y' is a string
of length 1, and it should only contain '1'. 'c_str()' for it
returns a string that contains '1' and '\0'.
The STL I'm using omit the zeroterm in this situation.
Chuck it. Get a decent implementation.
Klaus

#include <iostream>

using namespace std; //introduces namespace std

int main(){
string x("12");
string::iterator p(x.begin()),q(x.begin()+1);
string y(p,q);
const char *r=y.c_str();
cout <<r << " "<< int(r[0])<< " "<< int(r[1]);
return 0;
end;


"end;" ? What language is your native? :-)

Victor
Jul 19 '05 #2
klaus hoffmann schrieb:

[snip]

Thank you Victor, thank you Adam,

sorry for posting invalid code. Of course <string> was missing and the IDE seems
to have dropped the error msg somehow (problem with precompiled headers).
I had cut the (IMO) relevant part and somehow put in that "end;" (I used Pascal
in a former life).

sincerely
Klaus
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Murphy | last post by:
I've got an application that sends emails (not spam!). The application reads a template file (html/text) into a string, the string is processed and placeholders filled in as appropiate. I call a...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
2
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw...
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
4
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
2
by: vichoty | last post by:
Hi, I have a question on the following code: void foo(string p) { const char *cptr; { string str = "Hello" + p; cptr = str.c_str(); }
12
by: shyam | last post by:
Hi I have a program within which i have the following statement. sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str( ),SUFFIX.c_str( ) ); Here PREFIX, MIDDLE and SUFFIX are all const...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
8
by: puzzlecracker | last post by:
Any ideas what may cause that for the string class to core dump? #0 0x0018de92 in std::string::c_str () from /usr/lib/libstdc++.so.5
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.