Reset a string?

S

spike

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
 
L

Leor Zolman

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
(e-mail address removed)
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
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

spike said:
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
 
C

Christopher Benson-Manica

Tor Husabø said:
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";
 
R

Roberto Nunnari

Tor said:
spike said:
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:[email protected]
Residenza Boschetto 12 tel/fax: +41-91-6046511
6935 Bosco Luganese """ mobile: +41-76-3208561
Switzerland (o o)
========================oOO==(_)==OOo========================
 
C

Clark Cox

Tor Husabo said:
spike said:
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 */
 
E

Eric Sosman

Roberto said:
spike said:
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.
 
C

CBFalconer

Tor said:
spike said:
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.
 
J

Jack Klein

Tor said:
spike said:
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.
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

CBFalconer said:
Tor said:
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.
 
A

Adrian de los Santos

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.
 
M

Martin Dickopp

Tor Husabø said:
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
 
J

J. J. Farrell

Adrian de los Santos 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.
 
C

CBFalconer

Tor said:
Tor said:
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.
 
A

Adrian de los Santos

Adrian de los Santos 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.
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Martin said:
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.
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
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Adrian said:
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
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Tor said:
Adrian said:
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...
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Adrian said:
> 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
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Tor said:
Adrian said:
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 :/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top