China Blue Veins
5/14/2011 11:45:00 AM
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....