Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 73255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:10:22+00:00 2026-05-10T20:10:22+00:00

char *strtok(char *s1, const char *s2) repeated calls to this function break string s1

  • 0

char *strtok(char *s1, const char *s2)

repeated calls to this function break string s1 into ‘tokens’–that is the string is broken into substrings, each terminating with a ‘\0’, where the ‘\0’ replaces any characters contained in string s2. The first call uses the string to be tokenized as s1; subsequent calls use NULL as the first argument. A pointer to the beginning of the current token is returned; NULL is returned if there are no more tokens.

Hi,

I have been trying to use strtok just now and found out that if I pass in a char* into s1, I get a segmentation fault. If I pass in a char[], strtok works fine.

Why is this?

I googled around and the reason seems to be something about how char* is read only and char[] is writeable. A more thorough explanation would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:10:22+00:00Added an answer on May 10, 2026 at 8:10 pm

    What did you initialize the char * to?

    If something like

    char *text = 'foobar'; 

    then you have a pointer to some read-only characters

    For

    char text[7] = 'foobar'; 

    then you have a seven element array of characters that you can do what you like with.

    strtok writes into the string you give it – overwriting the separator character with null and keeping a pointer to the rest of the string.

    Hence, if you pass it a read-only string, it will attempt to write to it, and you get a segfault.

    Also, becasue strtok keeps a reference to the rest of the string, it’s not reeentrant – you can use it only on one string at a time. It’s best avoided, really – consider strsep(3) instead – see, for example, here: http://www.rt.com/man/strsep.3.html (although that still writes into the string so has the same read-only/segfault issue)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer jQuery's data does not keep a reference to the element… May 11, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer Check out the System.Windows.Forms.SendKeys class. You can use the static… May 11, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer Darn, I hate it when I prove myself wrong... it… May 11, 2026 at 9:52 pm

Related Questions

I have developed my own version of strtok. Just to practice the use of
Briefly: I'm after the equivalent of .NET's String.Trim in C using the win32 and
I am faced with the need to pull out the information in a string
I've been trying to get this code to work for hours! All I need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.