[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

I need help

janus

5/14/2011 11:06:00 AM

Hello All,

I found this is a code I am currently studying, could someone explain it to me.

char * string = "Simulated Annealing = 12847369";
char * value = strchr(string, '=');

*(value ++ ) = 0;


printf("%s ==== %s", value, string);

I noticed that sting will print in a new line, why this?

char* karmarkar = "Karmarkar 958572";
I want to use strchr to find the first occurrence of "space" in string karmarkar.
How do I do that?

Regards,
Janus

41 Answers

Dave \Crash\ Dummy

5/14/2011 11:31:00 AM

0


"janus" <emekamicro@gmail.com> schrieb im Newsbeitrag
news:04ee4fd1-29ef-4640-a906-22055187c1e4@glegroupsg2000goo.googlegroups.com...
> Hello All,
>
> I found this is a code I am currently studying, could someone explain it
> to me.
>
> char * string = "Simulated Annealing = 12847369";
> char * value = strchr(string, '=');

strchr returns a char* pointing to the '=' within string:

> *(value ++ ) = 0;

I'm not quite sure about the preceedence of operations. But I think,
first '=' is replaced by '\0', thus truncating string. value would then be
"",
but is advanced with ++ by on character in string, so pointing now to "
12847369"

> printf("%s ==== %s", value, string);

Using the explanation above I expect it to print
"Simulated Annealing ==== 12847369"

> I noticed that sting will print in a new line, why this?

I do not understand.

> char* karmarkar = "Karmarkar 958572";
> I want to use strchr to find the first occurrence of "space" in string
> karmarkar.
> How do I do that?

strchr(karmarkar, ' ')

> Regards,
> Janus

kind regards
Heiner

Joachim Schmitz

5/14/2011 11:40:00 AM

0

janus wrote:
> Hello All,
>
> I found this is a code I am currently studying, could someone explain
> it to me.
>
> char * string = "Simulated Annealing = 12847369";
> char * value = strchr(string, '=');
>
> *(value ++ ) = 0;

This should really result in a segfault, you're writing into a read only
('const') string.

>
>
> printf("%s ==== %s", value, string);
>
> I noticed that sting will print in a new line, why this?

Guess this is undefined behavoir and bad luck. I had better luck: the
program aborted.
Make the first line look like this and it should work properly:

char string[] = "Simulated Annealing = 12847369";

>
> char* karmarkar = "Karmarkar 958572";
> I want to use strchr to find the first occurrence of "space" in
> string karmarkar. How do I do that?

char * value = strchr(karmarkar, " ");

Bye, Jojo


Joachim Schmitz

5/14/2011 11:44:00 AM

0

Joachim Schmitz wrote:
> janus wrote:
>> Hello All,
>>
>> I found this is a code I am currently studying, could someone explain
>> it to me.
>>
>> char * string = "Simulated Annealing = 12847369";
>> char * value = strchr(string, '=');
>>
>> *(value ++ ) = 0;
>
> This should really result in a segfault, you're writing into a read
> only ('const') string.
>
>>
>>
>> printf("%s ==== %s", value, string);
>>
>> I noticed that sting will print in a new line, why this?
>
> Guess this is undefined behavoir and bad luck. I had better luck: the
> program aborted.
> Make the first line look like this and it should work properly:
>
> char string[] = "Simulated Annealing = 12847369";
>
>>
>> char* karmarkar = "Karmarkar 958572";
>> I want to use strchr to find the first occurrence of "space" in
>> string karmarkar. How do I do that?
>
> char * value = strchr(karmarkar, " ");

Sorry, I meant
char * value = strchr(karmarkar, ' ');

Bye, Jojo


China Blue Veins

5/14/2011 11:45:00 AM

0

In article
<04ee4fd1-29ef-4640-a906-22055187c1e4@glegroupsg2000goo.googlegroups.com>,
janus <emekamicro@gmail.com> wrote:

> Hello All,
>
> I found this is a code I am currently studying, could someone explain it to
> me.
>
> char * string = "Simulated Annealing = 12847369";
> char * value = strchr(string, '=');
>
> *(value ++ ) = 0;

This converts "Simulated Annealing = 12847369" to "Simulated Annealing \0
12847369". However modifying a string constant is not defined in the standard
and has implementation dependent result. To do this safely the string has to be
writable, such as,
char string[] = "Simulated Annealing = 12847369";
which is a char array variable, not a pointer to a string constant.

string points to the C string "Simulated Annealing ".
value points to the C string " 12847369".

> printf("%s ==== %s", value, string);
>
> I noticed that sting will print in a new line, why this?

You can see if the behaviour disappears when you use safe code.

> char* karmarkar = "Karmarkar 958572";
> I want to use strchr to find the first occurrence of "space" in string
> karmarkar.
> How do I do that?

If you want to search for the space character ' ', just do
strchr(karmarkar, ' ')
If you want the actual substring "s" "p" "a" "c" "e", use
strstr(karmarkar, "space")

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. In 1492....

Joachim Schmitz

5/14/2011 11:46:00 AM

0

Heinrich Wolf wrote:
> "janus" <emekamicro@gmail.com> schrieb im Newsbeitrag
> news:04ee4fd1-29ef-4640-a906-22055187c1e4@glegroupsg2000goo.googlegroups.com...
>> Hello All,
>>
>> I found this is a code I am currently studying, could someone
>> explain it to me.
>>
>> char * string = "Simulated Annealing = 12847369";
>> char * value = strchr(string, '=');
>
> strchr returns a char* pointing to the '=' within string:
>
>> *(value ++ ) = 0;
>
> I'm not quite sure about the preceedence of operations. But I think,
> first '=' is replaced by '\0', thus truncating string. value would
> then be "",
> but is advanced with ++ by on character in string, so pointing now to
> " 12847369"

That's how I read it too

>> printf("%s ==== %s", value, string);
>
> Using the explanation above I expect it to print
> "Simulated Annealing ==== 12847369"

It does print " 12847369 ==== Simulated Annealing ",
after having fixed the 1st line...

Bye, Jojo


China Blue Veins

5/14/2011 11:49:00 AM

0

In article <iqlp5a$q4m$1@news.m-online.net>,
"Heinrich Wolf" <invalid@invalid.invalid> wrote:

> "janus" <emekamicro@gmail.com> schrieb im Newsbeitrag
> news:04ee4fd1-29ef-4640-a906-22055187c1e4@glegroupsg2000goo.googlegroups.com..
> .
> > Hello All,
> >
> > I found this is a code I am currently studying, could someone explain it
> > to me.
> >
> > char * string = "Simulated Annealing = 12847369";
> > char * value = strchr(string, '=');
>
> strchr returns a char* pointing to the '=' within string:
>
> > *(value ++ ) = 0;
>
> I'm not quite sure about the preceedence of operations. But I think,
> first '=' is replaced by '\0', thus truncating string. value would then be
> "",
> but is advanced with ++ by on character in string, so pointing now to "
> 12847369"
>
> > printf("%s ==== %s", value, string);
>
> Using the explanation above I expect it to print
> "Simulated Annealing ==== 12847369"

Actually the transpose
1284736 === Simulated Annealing

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. In 1492....

Dave \Crash\ Dummy

5/14/2011 12:21:00 PM

0


"Joachim Schmitz" <jojo.nospam@schmitz-digital.de> schrieb im Newsbeitrag
news:iqlm9g$rs0$1@usenet01.boi.hp.com...
....
> It does print " 12847369 ==== Simulated Annealing ",
> after having fixed the 1st line...

Sure! I made a mistake.

Dave \Crash\ Dummy

5/14/2011 12:24:00 PM

0


"Joachim Schmitz" <jojo.nospam@schmitz-digital.de> schrieb im Newsbeitrag
news:iqlltc$rod$1@usenet01.boi.hp.com...
....
>> char * string = "Simulated Annealing = 12847369";
>> char * value = strchr(string, '=');
>>
>> *(value ++ ) = 0;
>
> This should really result in a segfault, you're writing into a read only
> ('const') string.

You are right! I missed that.

....
>> char* karmarkar = "Karmarkar 958572";
>> I want to use strchr to find the first occurrence of "space" in
>> string karmarkar. How do I do that?
>
> char * value = strchr(karmarkar, " ");

char * value = strstr(karmarkar, " ");
or
char * value = strchr(karmarkar, ' ');


James Kuyper

5/14/2011 1:15:00 PM

0

On 05/14/2011 07:39 AM, Joachim Schmitz wrote:
> janus wrote:
....
>> char * string = "Simulated Annealing = 12847369";
>> char * value = strchr(string, '=');
>>
>> *(value ++ ) = 0;
>
> This should really result in a segfault, you're writing into a read only
> ('const') string.

It's not 'const'. Attempting to write to it does have explicitly
undefined behavior, (6.4.5p6). However, that's completely separate from
the fact that attempting to write to an object defined with 'const' is
undefined behavior (6.7.3p5).

The fact that the behavior is undefined doesn't mean that it "should
segfault". If the standard did say that it "should segfault", that would
correspond to defining the behavior. In point of fact, one of the single
most common ways programs behave, when their behavior is undefined, is
in precisely the way their author inappropriately expects them to behave.

--
James Kuyper

Eric Sosman

5/14/2011 1:40:00 PM

0

On 5/14/2011 7:05 AM, janus wrote:
> Hello All,
>
> I found this is a code I am currently studying, could someone explain it to me.
>
> char * string = "Simulated Annealing = 12847369";
> char * value = strchr(string, '=');
>
> *(value ++ ) = 0;
>
>
> printf("%s ==== %s", value, string);
>
> I noticed that sting will print in a new line, why this?

Another thing it is likely to print is "SIGSEGV" or "SIGBUS" or
"General Protection Fault" or something along those lines. The
anonymous array created by the string literal is not `const', but
that's just an accident of history. Trying to modify such an array
yields undefined behavior -- and indeed, plenty of compilers will
put the arrays in read-only memory. (This allows them to save space
by allocating just one array to hold both of "over" and "recover".)

If you want a modifiable string, use the literal as an initializer:

char string[] = "Simulated Annealing = 12847369";

This gives you an ordinary (named) array that you can modify at will,
initialized with the characters of the string literal.

> char* karmarkar = "Karmarkar 958572";
> I want to use strchr to find the first occurrence of "space" in string karmarkar.
> How do I do that?

char *space = strchr(karmarkar, ' ');
if (space == NULL)
printf ("No spaces\n");
else
printf ("First space is at position %u\n",
(unsigned)(space - karmarkar));

--
Eric Sosman
esosman@ieee-dot-org.invalid