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 7537951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:59:10+00:00 2026-05-30T06:59:10+00:00

I am having trouble with the strtok function. I keep getting a ‘bus error.’

  • 0

I am having trouble with the strtok function. I keep getting a ‘bus error.’ I wrote a function to return all the words within a line. Could somebody please point out my error?

NOTE: I am used to higher level languages

void extract_words(char tokens[WORD_MAX][WORD_LEN], char* line, int* sizePtr)
{
  printf("in extract words");   //for debugging

  char* chPtr = NULL;
  chPtr = strtok(line, " ");

  int size = 1;                             //words has one element
  while(chPtr != NULL) 
  {  
     strcpy(tokens[size++], chPtr);
     chPtr = strtok(NULL, " ");               //continue to tokenize the string
  }
  *sizePtr = size;
}

Thanks in advance!

  • 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. Editorial Team
    Editorial Team
    2026-05-30T06:59:11+00:00Added an answer on May 30, 2026 at 6:59 am

    strtok modifies the string you pass to it, so it can’t be a string literal. You should be able to do something like this:

    void extract_words(char tokens[WORD_MAX][WORD_LEN], const char* line_arg, int* sizePtr)
    {
      char line[(WORD_LEN+1)*WORD_MAX];
      char* chPtr = NULL;
      int size = 0;
    
      strcpy(line,line_arg);
      printf("in extract words");   //for debugging
    
    
      chPtr = strtok(line, " ");
    
      while(chPtr != NULL) 
      {  
         strcpy(tokens[size++], chPtr);
         chPtr = strtok(NULL, " ");               //continue to tokenize the string
      }
      *sizePtr = size;
    }
    

    Note that I also initialized size to zero, since array indices start at zero.

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

Sidebar

Related Questions

Having trouble with this jQuery function: $(#content).siblings().each(function(i){ heightOfSiblings = heightOfSiblings + this.outerHeight(); }); Error
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having trouble looping through multiple rows in a SQL table and getting the info
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
Having trouble getting JQuery to access the parent form field 'inputStart's value when a
Having trouble getting the correct bounds for my iPad application when launching it in
Having trouble creating my swap chain. I receive the following error. DX3dApp.obj : error
I am having a bit of trouble using strtok with strcmp. //Handles the header
Having trouble getting the oauth access token and secret exchanged from the request tokens
Having trouble getting the javascript alert to display from my code behind. c# -

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.