Re: strtok, bus error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux