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

Home Posts Topics Members FAQ

Reset a string?

How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..

Thanx
Nov 14 '05 #1
22 17204
On 5 Mar 2004 12:46:40 -0800, jo**@ljungh.se (spike) wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..

Thanx


Easy: put a '\0' into the first position. Whether "s" is defined as a
pointer-to-char or an array-of-char, you just say:
*s = '\0';
or
s[0] = '\0';

But beware that is isn't a pointer-to-const-char; doing this may invoke
undefined behavior.

Also note that this doesn't "empty" the string, it just marks the first
position as the terminating NUL. The memory it was using is still there and
needs to be contended with.
-leor


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
spike wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..


char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */

Now the string is considered empty, and strlen(str) will return zero.

Tor
Nov 14 '05 #3
Tor Husabø <to***@student. hf.uio.no> spoke thus:
char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */


As Leor's post notes, this is incorrect code: str points to a string
literal, and thus may not be modified. You wanted something like

char str[]="hello";

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
Tor Husabø wrote:
spike wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..

char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */


be careful with char pointers like that.. it's not portable.
you have no garanties you'll be able to modify it like you did..
the compiler may put "hello" in readonly memory..

a correct version should be:

char str[] = "hello";
str[0] = '\0';

Best regards.
--
Roberto Nunnari -software engineer-
mailto:ro**@nun nisoft.ch
Residenza Boschetto 12 tel/fax: +41-91-6046511
6935 Bosco Luganese """ mobile: +41-76-3208561
Switzerland (o o)
=============== =========oOO==( _)==OOo======== =============== =

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #5
In article <0O************ *******@news2.e .nsc.no>,
Tor Husabo <to***@student. hf.uio.no> wrote:
spike wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..


char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */


That's undefined behavior, I bealieve that you meant (Notice the change
in the first line):

char str[] = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */

Nov 14 '05 #6
Roberto Nunnari wrote:

Tor Husabø wrote:
spike wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..

char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */


be careful with char pointers like that.. it's not portable.
you have no garanties you'll be able to modify it like you did..
the compiler may put "hello" in readonly memory..


Even if no read-only memory is involved you can find
yourself in trouble. For example,

char *str = "hello";
str[0] = '\0';
puts ("Some rhymes: bellow, fellow, Jell-O, and hello");

may well produce the output

Some rhymes: bellow, fellow, Jell-O, and

More than one compiler performs the optimization that
produces this effect when abused by incorrect code.

--
Er*********@sun .com
Nov 14 '05 #7
Tor Husabø wrote:
spike wrote:
How do i reset a string? I just want to empty it som that it
does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..


char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */

Now the string is considered empty, and strlen(str) will return
zero.


Nope. Now it has undefined (or possibly implementation defined)
behaviour, which may include going belly up. Think about it for a
while.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #8
On Fri, 05 Mar 2004 22:12:15 +0100, Roberto Nunnari
<ro**@nunnisoft .ch> wrote in comp.lang.c:
Tor Husabø wrote:
spike wrote:
How do i reset a string?
I just want to empty it som that it does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..

char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */


be careful with char pointers like that.. it's not portable.
you have no garanties you'll be able to modify it like you did..
the compiler may put "hello" in readonly memory..

a correct version should be:

char str[] = "hello";
str[0] = '\0';

Best regards.


It's more than not portable, it is undefined behavior.

The C standard (all versions) does not state that string literals are
read-only, or whether or not they may share storage. Attempting to
modify string literals in C is undefined behavior because the C
standard (all versions) specifically says it is.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
CBFalconer wrote:
Tor Husabø wrote:
spike wrote:

How do i reset a string? I just want to empty it som that it
does not contain any characters

Say it contains "hello world" at the time...
I want it to contain "". Nothing that is..


char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */

Now the string is considered empty, and strlen(str) will return
zero.

Nope. Now it has undefined (or possibly implementation defined)
behaviour, which may include going belly up. Think about it for a
while.


I know, just hasn't written C code for a while.

Maybe I should blame the fact that the two declarations below are equal?
Maybe it's more confusing than really helpful to differentiate in this way?
void func1(int a[]); /* argument is pointer to array */
void func2(int *a); /* argument is pointer to a single int */

I guess this could be what had me fooled for a moment.
Nov 14 '05 #10

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

Similar topics

6
36545
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
2
2650
by: Charles M. Fish, Sr. | last post by:
I’m so tired from banging this problem around all day, I hope I can explain it succinctly & accurately. I want to execute a function immediately following a click on <input type="RESET"... The function will insert today’s date into a <input type="TEXT"… field. It does it just fine with <form onload="insertDate();" where inside the ‘insertDate’ fn, I have document.form.dateField.value = dateString. The closest I’ve been able to do...
18
6652
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset the loop. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
1
14383
by: NancyASAP | last post by:
Thought I'd share this since it took me a long time to get it working. Thanks to a bunch of contributers in Google Groups who shared javascript, etc. The question was: How can I put a reset button on my ASP.NET web page, and have an HTML reset button click clear 1) all validator text display and 2) validation summary display. Problem was clearing them and yet still leaving them working and visible if the user immediately began...
4
5334
by: Lee Chapman | last post by:
Hi, Can anyone tell me why in the code below, the call to ClearChildViewState() has no effect? To paraphrase the code: I'm using view state. I have a textbox and a submit button (and a label that can be ignored). When I press the button the first time, the click handler hides the textbox. Pressing the button a second time unhides the textbox. The text box is maintaining its value when hidden via view state. (The value is NOT being...
5
5994
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset): Public Class DatePicker2...
7
14643
by: Kermit Piper | last post by:
Hello, How can you clear session variables when a reset button is pressed? I thought I might be able to do something like: <% If request.form("Reset") = "Reset" then Session("variable") = Null %>
2
5135
by: wvtempl | last post by:
From the documentation in MSDN, it looks as though the following should iterate through the collection twice. However, MoveNext in the second iteration returns false: Dim oList As New List(Of String) oList.Add("Hello") oList.Add("world") Dim oEnum As List(Of String).Enumerator = oList.GetEnumerator() While oEnum.MoveNext() Dim sValue As String = oEnum.Current End While
6
2078
by: RNEELY | last post by:
I've inherited code similar to the following with a comment on resetting the string. Why would anyone want to do this? How could this reset the string? What does it mean to reset a string? Public Sub SetTheStringToSomething(ByRef OutStr As String) OutStr = "123" End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
6
9933
by: jarice1978 | last post by:
Hello, I have been scanning the internet for a few days now. That is not working. So now it is time to post! I have read a few other posts on here about authentication but they do not match exactly. We currently have an intranet app built in a mixture of asp and asp.net 1.1 and 2.0 written in VB .Net. We have a form where the user logs in and it authenticates against active directory successully in 2 ways: 1. The admin resets the...
0
9680
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
10456
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, 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...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7548
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
5442
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.