By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

changing a string

P: n/a
Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
Can I just change the value of s[2] to '\0'?

Thanks a lot
Sara
Oct 12 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
sa*******@gmail.com wrote:
Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";
"abcd" is a string literal, it is a constant so you can't change it.
You can try, but the result is undefined.

If you want to manipulate strings, use std::string.

--
Ian Collins
Oct 12 '08 #2

P: n/a
sa*******@gmail.com wrote:
Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
Using std::string.
Can I just change the value of s[2] to '\0'?
No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; // (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].
Best

Kai-Uwe Bux
Oct 12 '08 #3

P: n/a
On Oct 12, 2:41*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

* char const * s = "abcd"; * * *// (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

* char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux
What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Oct 12 '08 #4

P: n/a
sa*******@gmail.com wrote:
On Oct 12, 2:41*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; * * *// (*)

it would be obvious that you are trying to modify something declared
const. This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux

What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
Then you are not 0-terminated. Consequently, many function calls to standard
C-string functions will have undefined behavior.

Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Not that I would be aware of. But now you could do s[2] = \0. That, of
course, would not erase a block in the middle.
What is your reason not to use std::string?
Best

Kai-Uwe Bux
Oct 12 '08 #5

P: n/a
Kai-Uwe Bux wrote:
sa*******@gmail.com wrote:
[..]
>Is there any built-in method to erase a part of string defined as
char[] instead of std::string?

Not that I would be aware of. But now you could do s[2] = \0. That, of
course, would not erase a block in the middle.
Nit-pick: now you could do

s[2] = 0

or

s[2] = '\0'

(You could of course put a backslash between the space and the zero, but
there has to be a line break immediately following the backslash <g>)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 13 '08 #6

P: n/a
On Sun, 12 Oct 2008 14:22:01 -0700 (PDT), sa*******@gmail.com wrote:
>Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
This kind of string manipulation is more C than C++, but there is
still a library for working with these kinds of strings.

#include <cstring>

It's a bad idea to manipulate a string assigned from a literal,
though. That is...

char *s="abcd";
s[2] = 0; // Don't do this!

Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;

In the above, I cut the string short by writing a new null terminator
to the appropriate character position.

However, this kind of stuff is very error prone, and tends to be a
cause of crashes, memory corruption, security issues and more. It's
very easy to end up reading/writing past the end of your buffers and
so on. Seriously, just use the std::string class - you'll regret it if
you don't.

Oct 13 '08 #7

P: n/a
On 2008-10-13 01:16, sa*******@gmail.com wrote:
On Oct 12, 2:41 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; // (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux

What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
If you must use char-arrays use

char s[] = "abcd";

this will ensure that the string is properly null-terminated.
Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Look up <cstringin your favourite C++ reference.

--
Erik Wikström
Oct 13 '08 #8

P: n/a
Stephen Horne wrote:
Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;
Given this is almost *exactly* replicating what std::string would do
(except that it would do it more safely), I really can't understand why
you are giving this as a viable solution. Why not give the solution
using std::string rather than this? The end result will be the same, but
much safer and more didactic.
Oct 13 '08 #9

P: n/a
On Mon, 13 Oct 2008 16:34:33 GMT, Juha Nieminen
<no****@thanks.invalidwrote:
>Stephen Horne wrote:
>Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;

Given this is almost *exactly* replicating what std::string would do
(except that it would do it more safely), I really can't understand why
you are giving this as a viable solution. Why not give the solution
using std::string rather than this? The end result will be the same, but
much safer and more didactic.
Read the rest of the post.

In short, I *didn't* give it as a viable solution, I made it pretty
clear that it *wasn't* a sensible solution. But that doesn't mean that
I take an "I know this but you are not worthy" attitude.

Take a "do what I say" attitude and people will ignore you and do what
they want anyway. So provide the facts and explain why it's a bad
idea. That way, when they ignore you and do it anyway, they don't get
to blame your attitude when it all goes horribly wrong.

Oct 14 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.