473,804 Members | 2,096 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
22 17208
On 2004-03-05 15:46:32 -0600, Eric Sosman <Er*********@su n.com> said:
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.

Intresting.. very intresting, but why the compiler do this ?

Thanks.

Nov 14 '05 #11
Tor Husabø <to***@student. hf.uio.no> writes:
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 */
No, a pointer to an array cannot be used as an argument here, but a
pointer the first element of an array can.
void func2(int *a); /* argument is pointer to a single int */


Both declarations are equivalent. In both cases, `a' is pointer to an
object of type `int'. Whether this object is a single `int' or the first
element of an array makes no difference as far as the declaration is
concerned.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #12

"Adrian de los Santos" <de*********@de mon.com.mx> wrote in message news:c2******** *****@ID-86516.news.uni-berlin.de...
On 2004-03-05 15:46:32 -0600, Eric Sosman <Er*********@su n.com> said:

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.


Intresting.. very intresting, but why the compiler do this ?


Because attempting to modify a string literal constant
results in undefined behaviour, and the compiler can do
what it wants. Since the writer of the code is not allowed
to invoke undefined behaviour if he wants predictable
results, the compiler can assume that he doesn't do so.
Hence the compiler can assume that the string literal
"hello" will never be modified, and hence it can optimise
memory usage by re-using the string "hello" that occurs
at the end of the bigger string. If the coder does risk
his life and sanity by modifying the string literal, in
this implementation he modifies others at the same time.
Nov 14 '05 #13
Tor Husabø wrote:
Tor Husabø wrote:
spike wrote:
.... snip ...
char *str = "hello";
str[0] = '\0'; /* put a null terminator at the beginning */
.... snip ...
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.


If the original declaration is of the (correct) form:

const char *str = "hello";

then the other misusages should get flagged at compile time.

--
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 #14
On 2004-03-06 06:14:19 -0600, "J. J. Farrell" <jj*@bcs.org.uk > said:

"Adrian de los Santos" <de*********@de mon.com.mx> wrote in message
news:c2******** *****@ID-86516.news.uni-berlin.de...
On 2004-03-05 15:46:32 -0600, Eric Sosman <Er*********@su n.com> said:

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.


Intresting.. very intresting, but why the compiler do this ?


Because attempting to modify a string literal constant
results in undefined behaviour


fist pardon me please for my poor knowledge of this topics.

As far as i understand

char *str="hello";

- will create a pointer to a char str
- will store "hello" on the memory
- will store the address of memory where "hello" starts in the str variable.

Am i right ?

So why *str becomes a constant ?
because it was assigned a rvalue at initialization ? (="hello")

Thanks for your kind explanation.

Nov 14 '05 #15
Martin Dickopp wrote:
Tor Husabø <to***@student. hf.uio.no> writes:

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 */

No, a pointer to an array cannot be used as an argument here, but a
pointer the first element of an array can.

Okay, maybe it's the wrong term here.
void func2(int *a); /* argument is pointer to a single int */

Both declarations are equivalent. In both cases, `a' is pointer to an
object of type `int'. Whether this object is a single `int' or the first
element of an array makes no difference as far as the declaration is
concerned.

That's my point, exactly. Allowing this to be written in both ways is a
bit confusing, since the char* and char[] notations have different
meanings in other contexts, but here they are equivalent. The
comments int the code are supposed to say what the perceived
difference is, it's only for documentation purposes.

Guess you didn't read my post properly (and I wasn't making myself
very clear, either).

Tor
Nov 14 '05 #16
Adrian de los Santos wrote:
As far as i understand

char *str="hello";

- will create a pointer to a char str
- will store "hello" on the memory
- will store the address of memory where "hello" starts in the str
variable.

Am i right ?

So why *str becomes a constant ?
because it was assigned a rvalue at initialization ? (="hello")


char *str does not become a constant! It's "hello" that is "constant"
(in the sense that modifying it is has undefined behavior, even
if it might work on your implementation) .

This is allowed, however:

char other_string[] = "goodbye";
char *str="hello";
str = other_string;

now str points to "goodbye", and "hello" is just wasted memory, since
you dont have the address anymore.

Read all about it here: http://www.eskimo.com/~scs/C-faq/q1.32.html
Nov 14 '05 #17
Tor Husabø wrote:
Adrian de los Santos wrote:
As far as i understand

char *str="hello";

- will create a pointer to a char str
- will store "hello" on the memory
- will store the address of memory where "hello" starts in the str
variable.

Am i right ?

So why *str becomes a constant ?
because it was assigned a rvalue at initialization ? (="hello")

char *str does not become a constant! It's "hello" that is "constant"
(in the sense that modifying it is has undefined behavior, even
if it might work on your implementation) .

This is allowed, however:

char other_string[] = "goodbye";
char *str="hello";
str = other_string;

now str points to "goodbye", and "hello" is just wasted memory, since
you dont have the address anymore.

Read all about it here: http://www.eskimo.com/~scs/C-faq/q1.32.html


Sorry, this one got posted at the wrong place in the thread...
Nov 14 '05 #18
Adrian de los Santos wrote:
As far as i understand

char *str="hello";

- will create a pointer to a char str
- will store "hello" on the memory
- will store the address of memory where "hello" starts in the str variable.
Am i right ?

So why *str becomes a constant ?
because it was assigned a rvalue at initialization ? (="hello")

char *str does not become a constant! It's "hello" that is "constant"
(in the sense that modifying it is has undefined behavior, even
if it might work on your implementation) .

This is allowed, however:

char other_string[] = "goodbye";
char *str="hello";
str = other_string;

now str points to "goodbye", and "hello" is just wasted memory, since
you dont have the address anymore.

Read all about it here: http://www.eskimo.com/~scs/C-faq/q1.32.html

Tor
Nov 14 '05 #19
Tor Husabø wrote:
Adrian de los Santos wrote:
> As far as i understand
>
> char *str="hello";
>
> - will create a pointer to a char str
> - will store "hello" on the memory
> - will store the address of memory where "hello" starts in the str

variable.
>
> Am i right ?
>
> So why *str becomes a constant ?
> because it was assigned a rvalue at initialization ? (="hello")

char *str does not become a constant! It's "hello" that is "constant"
(in the sense that modifying it is has undefined behavior, even
if it might work on your implementation) .

This is allowed, however:

char other_string[] = "goodbye";
char *str="hello";
str = other_string;

now str points to "goodbye", and "hello" is just wasted memory, since
you dont have the address anymore.

Read all about it here: http://www.eskimo.com/~scs/C-faq/q1.32.html

Tor


Oops, did it again :/
Nov 14 '05 #20

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

Similar topics

6
36547
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
14387
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
5335
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
5995
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
14644
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
2081
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
9715
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
10600
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...
0
10352
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
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
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
7642
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...
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.