Re: strtok, bus error

ben <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
Fundu a écrit :
> i'm trying to do pretty simple replacement using strtok.
> but it looks like i have missed some subtle difference between the two following 
>   
>   char src[]  = "hello world #";
>   char *other = "hello world #";

i don't know if it is a compiler feature (storage behavior into the DATA
segment), or a linux kernel feature, or if it is specified in ANSI, but
the second way leads to pointing to a _constant_ string. If someone can
enlighten...

> because if i use "char * other" with strtok it fails with bus error but i use src it works, don't understand what's the difference.
> any insight would be appreciated, TIA!
strtok manpage says it has to _write_ into the string pointed to by its
first parameter. this is not allowed if the pointer points to a constant
string.

then,
you have to point into a string that have been created at runtime.

char* src;
src = malloc (12);
strncpy (src, "hello#world", 11);
...
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.