开发者

Learning C by doing string search

I found this in 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 strin开发者_如何学Gog will print in a new line first, 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?


  1. Nope, it doesn't put a new line http://codepad.org/xhTiV4Qn . Actually on my machine it gives sigsegv because you are trying to write at a read only location inside "Simulated ... ".
  2. int pos = strchr(karmarkar, ' ') - karmarkar;. strchr(karmarkar, ' ') returns a pointer to the first occurrence of " " (space).


The code first computes the location of the '=' character (as an address, not an index). Then the character at this address is set to 0 and the address of value is set to the next sign. This makes "string" end before the '=' (which is now a 0), as it is 0 terminated and value starts after the '='. So the printf will print out " 12847369 ==== Simulated Annealing " (notices that the spaces are still there).

For you second question it works similar, as Alexandru pointed out in his answer.


First of all

You define string to point to a constant string literal, which is stored in read-only memory:

char * string = "Simulated Annealing = 12847369";

Then you find the first occurrence of = in this string and put the pointer to it in value:

char * value = strchr(string, '=');

So value now points to a read-only memory or value = NULL, so this is a line that has undefined behavior:

*(value ++ ) = 0; // assign value to read-only memory

if let's say you wrote

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

You were storing this string in an array on the stack, and then you could've written to it.

Now, to your question

Your code will not print it to a new line, if you wrote anything before that, you'd see that:

char string[] = "Simulated Annealing = 12847369";
char * value = strchr(string, '=');
*(value ++ ) = 0;
printf("First line");
printf("%s ==== %s", value, string);

To find the first occurrence of the space character, use strchar(karmarkar, ' '):.

And once again, when you use strchr check if the return value is NULL, since it might be, and if it is NULL indeed then your program will probably crash. (dereferencing illegal address)


In the first case, strchr will return a pointer to the byte where an '=' is found in string. You need not append 0 to the returned value as it is just another pointer to the character array pointed to by string, which is already null terminated. To print the character, just say value[0]:

char* string = "Simulated Annealing = 12847369";
char* value = strchr(string, '=');
if (value != NULL)
  {
    printf("value: %s, string: %s\n", value, string);
    printf("char: %c\n", value[0]); /* prints = */
  }

Do the same to find a space in the second string:

char* karmarkar = "Karmarkar   958572";
value = strchr(karmarkar, ' ');
if (value != NULL)
  printf("value: %s\n", value);

Always check the return value of strchr before using it, as it returns NULL if the character is not found.

BTW, the statement, *(value ++) = 0 should cause a segfault as it attempts to modify a read-only location. If you found this code in a book that claims to teach C, I suggest that you move away from it and start learning from this. If your compiler generated code that prints a newline for that particular statement, you should start using a better compiler. There are some few good ones available for free download.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜