All of lore.kernel.org
 help / color / mirror / Atom feed
From: ben <brouits@free.fr>
To: linux-c-programming@vger.kernel.org
Subject: Re: strtok, bus error
Date: Tue, 17 Feb 2009 05:04:22 +0100	[thread overview]
Message-ID: <499A3746.1070502@free.fr> (raw)
In-Reply-To: <125035.76273.qm@web63404.mail.re1.yahoo.com>

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

  parent reply	other threads:[~2009-02-17  4:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-17  2:52 strtok, bus error Fundu
2009-02-17  3:57 ` Bryan Christ
2009-02-17  6:31   ` Fundu
2009-02-17  4:04 ` ben [this message]
2009-02-17 20:15   ` Glynn Clements
2009-02-17 21:05     ` ben

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=499A3746.1070502@free.fr \
    --to=brouits@free.fr \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.