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 9 1496 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 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
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? sa*******@gmail .com wrote:
On Oct 12, 2:41*pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>saraja...@gmai l.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 compatibilit y 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
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
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.
On 2008-10-13 01:16, sa*******@gmail .com wrote:
On Oct 12, 2:41 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>saraja...@gmai l.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 compatibilit y 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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Daniel |
last post by:
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?
|
by: Samridhi Kumar Shukla |
last post by:
I am facing technical difficulti in using server connection control because
we cannot change the path once fixed ..
one way is to edit the code of the form generated code but though it allow
to manipulate but again when i open the project one day latter the
connection string
cn.connection string goes away i have again write this code. is there any
way out for this
|
by: Ioannis Demetriades |
last post by:
Hi,
I need to change the printer's font to "control" -a printer font, and then
send a sequence of characters to the printer. My problem is that I cannot
change the printer's font. Can this be done without API calls?
Thanks
Ioannis
|
by: Darian |
last post by:
I am wondering how (if it is possible of course) I can change a users
screen resolution to a certain size when running my application and
then change it back when they exit my application.
For instance, if the users screen size is 800x600, I want the
resolution to change to 1260x780 when running my program and then
change back to 800x600 when exiting my program.
Thanks,
Darian
|
by: Tugrul HELVACI |
last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ??
I'm using Delphi 2006 and I have a class defination like this:
TPerson = class
fPersonName : String;
fPersonSurName : String;
fPersonAge : Integer;
published
property PersonName : String read fPersonName write fPersonName;
| |
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |