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

  • SEARCH
  • Home
  • 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 6376051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:41:42+00:00 2026-05-25T01:41:42+00:00

Well, I declared a global array of chars like this char * strarr[]; in

  • 0

Well, I declared a global array of chars like this char * strarr[];

in a method I am tokenising a line and try to put everything into that array like this

*line =  strtok(s, " ");

while (line != NULL) {
  *line = strtok(NULL, " ");
}

seems like this is not working.. How can I fix it?

Thanks

  • 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-25T01:41:42+00:00Added an answer on May 25, 2026 at 1:41 am

    Any number of things could be going wrong with the code you haven’t shown us, such as undefined behaviour by strtoking a string constatnt, or getting your parameters wrong when calling the function.

    But the most likely problem from the code we can see is the use of *line instead of line, assuming that line is of type char *.

    Use the following code as a baseline:

    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
        char str[] = "My name is paxdiablo";
    
        // Start tokenising words.
    
        char *line =  strtok (str, " ");
        while (line != NULL) {
            // Print current token and get next word.
    
            printf ("[%s]\n", line);
            line = strtok(NULL, " ");
        }
    
        return 0;
    }
    

    This outputs:

    [My]
    [name]
    [is]
    [paxdiablo]
    

    and should be easily modifiable into something you can use.

    Be aware that, if you’re trying to save the character pointers returned from strtok (which would make sense for using *line), they are transitory and will not be what you expect after you’re done. That’s because modifications are made in-place within the source string. You can do it with something like:

    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
        char *word[4];         // The array of words.
        size_t i;              // General counter.
        size_t nextword = 0;   // For preventing array overflow.
    
        char str[] = "My name is paxdiablo";
    
        // Start tokenising.
    
        char *line =  strtok (str, " ");
        while (line != NULL) {
            // If array not full, duplicate string to array and advance index.
    
            if (nextword < sizeof(word) / sizeof(*word))
                word[nextword++] = strdup (line);
    
            // Get next word.
    
            line = strtok(NULL, " ");
        }
    
        // Print out all stored words.
    
        for (i = 0; i < nextword; i++)
            printf ("[%s]\n", word[i]);
    
        return 0;
    }
    

    Note the specific size of the word array in that code above. The use of char * strarr[] in your code, along with the message tentative array definition assumed to have one element is almost certainly where the problem lies.

    If your implementation doesn’t come with a strdup, you can get a reasonably-priced one here 🙂

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

Sidebar

Related Questions

well i have this messages table with sample values like these: msg_id recipient_id read
I have a ContentProvider class and is declared in AndroidMenifest.xml like this: <provider android:name=.MediaSearchProvider
In C# and in Java (and possibly other languages as well), variables declared in
Well, this is my first post here and really enjoying the site. I have
Well this is incredibly frustrating. After being nagged by Rails that I need to
When a method is declared as virtual in a class, its overrides in derived
Well,I have a parent class with a nested class declared in the protected tab
I'm having some problems with ActiveRecord. Well everything works fine, but it's working sometimes.
I have recently read about how cursors should be avoided. Well, I would like
Well, I would like to build a file hosting website just like other already

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.