3

I have a functions which takes a char * as its only argument. I then perform some strtok operations on it. Sometimes it works and sometimes it doesent. It working depends upon how the string was constructed. For instance here are the two cases.

int main()
{
   char glob[] = "/abc/def/ghi";
   char *glob2 = "/abc/def/ghi";

   func(glob);  //this one works
   func(glob2); //this one doesnt work

   return 0;
}

What is the difference between the two allocation methods and why does strtok blow up on the second one?

2
  • 9
    Read litb's excellent post for the difference between char[] and char* - stackoverflow.com/questions/308279/c-strings-vs/308724#308724 Commented Aug 4, 2009 at 14:11
  • 3
    Assigning a string constant to a char* (instead of a const char*) is only legal for historical reasons. It is considered deprecated, and some compilers will issue you a warning about it. Commented Aug 4, 2009 at 14:13

4 Answers 4

12

strtok() basically modifies the input string.

char *glob2 = "/abc/def/ghi";

In above case the glob2 points to read-only data and hence it fails, whereas with 'char glob[] = "/abc/def/ghi";' the data is not read-only, it's available in char array. Hence it allows the modifications.

Sign up to request clarification or add additional context in comments.

1 Comment

Quoting from the man page: Be cautious when using these functions. If you do use them, note that: - These functions modify their first argument. - These functions cannot be used on constant strings. - The identity of the delimiting character is lost. - The strtok() function uses a static buffer while parsing, so it’s not thread safe. Use strtok_r() if this matters to you.
7

char[] str1 = "foo" allocates an array of the chars on the stack (assuming this is inside a function). That array can be modified without a problem.

const char *str = "foo" gives you a pointer to the string foo, which will usually reside in read-only memory.

char *str = "foo" will do the same thing but implicitly remove the const (without actually changing the fact that the pointer likely points to read-only memory).

Comments

6

Strtok writes to the memory allocated to the string.

You cannot write to statically allocated string memory on most compilers/runtimes/hardware. You can write to the stack.

Comments

-1

The other comments are correct; you should use strtok_r() instead.

1 Comment

-1, since strtok_r doesn't actually relate directly to the problem here: Both strtok_r and strtok modify the input buffer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.