473,699 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'\0' in a C++ string?

Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C. If this is the case (please tell
me if it isn't), it appears that the easiest way to treat a C++ string as a
char * so it can be used with functions requiring a character pointer such
as strcmp (I realize that one would not normally want to do this), etc. is
to use the form: &stringObjec t[0] (is there a better way?). In MFC, one
need to merely cast the CString type to a char *, but this doesn't appear to
be the case with a standard C++ string.

Thanks,
Ray Mitchell
Jul 22 '05 #1
4 1555
Ray Mitchell wrote:
Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C. If this is the case (please tell
me if it isn't), it appears that the easiest way to treat a C++ string as a
This is only the case when you get the string using the c_str member
function.
char * so it can be used with functions requiring a character pointer such
as strcmp (I realize that one would not normally want to do this), etc. is
to use the form: &stringObjec t[0] (is there a better way?). In MFC, one
No! String elements are not guarenteed contiguous. Use c_str(), that's
what it's there for.
need to merely cast the CString type to a char *, but this doesn't appear to
be the case with a standard C++ string.


It isn't. Also note that it's perfectly legal for a std::string to have
multiple '\0' characters inside. The string isn't just chopped off at
the first '\0'.

Jacques.
Jul 22 '05 #2
Ray Mitchell wrote:
Hello,

My understanding is that C++ string class objects still terminate
their strings with a '\0', as do strings in C.
They might do this internally, or they might not.
If this is the case (please tell me if it isn't), it appears that the
easiest way to treat a C++ string as a char * so it can be used with
functions requiring a character pointer such as strcmp (I realize that
one would not normally want to do this), etc. is to use the form:
&stringObjec t[0] (is there a better way?).
This is not guaranteed to work. If you need a C style string, use the
member function c_str().
In MFC, one need to merely cast the CString type to a char *, but this
doesn't appear to be the case with a standard C++ string.


That's right. It would be dangerous to support this, therefore it's not
allowed.

Jul 22 '05 #3
In article <Uv************ ******@newsread 1.news.pas.eart hlink.net>,
"Ray Mitchell" <Ra*********@Me anOldTeacher.co m> wrote:
Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C.


No they do not:

string s( "abc\000def " );
assert( s.size() == 7 );
Jul 22 '05 #4
In article <Uv************ ******@newsread 1.news.pas.eart hlink.net>,
"Ray Mitchell" <Ra*********@Me anOldTeacher.co m> wrote:
My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C.


Let me try that again.

int main() {
string s;
for ( int x = 0; x < 100; ++x )
s.push_back( '\0' );
assert( s.size() == 100 );
}
Jul 22 '05 #5

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

Similar topics

16
6739
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31176
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it will split myString up using the delimiter of 1 space so that
9
8000
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
10
8168
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
4778
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
4310
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
2
3175
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was a tasty burger"
15
50247
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
3064
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
8
4736
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.