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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:50:25+00:00 2026-06-12T02:50:25+00:00

I am not the best with pointers, so maybe you can see what I’m

  • 0

I am not the best with pointers, so maybe you can see what I’m doing wrong.

Let’s say that I have an array that was initialized like this:

char *arrayOfCommands[]={"ls -l", "wc -l"};

My goal is to get an array called char *currentCommand out of this array that looks at a specific cell of arrayOfCommands and separates the command into pieces on spaces.

My final goal would be to have a new currentCommand array on each loop that each look like this:

First Loop:  
currentCommand = [ls][-l]

First Loop:  
currentCommand = [wc][-l]

Here is the code I have so far:

for (i = 0; i < 2; ++i) {
    char str[] = arrayOfCommands[i];
    char * currentCommand;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    currentCommand = strtok (str, " ");
    while (currentCommand != NULL){
        printf ("%s\n",currentCommand);
        currentCommand = strtok (NULL, " ");
    }

    .
    .
    .

    //Use the currentCommand array (and be done with it)
    //Return to top
}

Any help would be greatly appreciated! 🙂

UPDATE:

for (i = 0; i < commands; ++i) {
    char str[2];
    strncpy(str, arrayOfCommands[i], 2);
    char *currentCommand[10];
    printf ("Splitting string \"%s\" into tokens:\n",str);
    currentCommand = strtok (str, DELIM);
    while (currentCommand != NULL){
        printf ("%s\n",currentCommand);
        currentCommand = strtok (NULL, DELIM);
    }
}

I am getting this error: ** incompatible types in assignment**
It’s talking about the “str” I’m passing the strtok function.

  • 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-06-12T02:50:26+00:00Added an answer on June 12, 2026 at 2:50 am

    strtok operates by modifying the string that you pass; this is easy to miss when using some man pages. Each command in your array is a literal string: attempts to modify them will cause problems. So you’ll need to make a copy of each command before using it with strtok.

    Furthermore, this is an invalid initialization for an array:

    char str[] = arrayOfCommands[i];
    

    Declare str as an array of some fixed size, then use strncpy to make copies of each command before tokenizing them using strtok:

    char str[MAX_COMMAND_LEN + 1];
    strncpy(str, arrayOfCommands[i], MAX_COMMAND_LEN);
    
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe my title is not the best but here is what I really want.I
From what I have understood, it is not the best way to open a
Ok, so maybe my search syntax is wrong here, this could have very well
I have a faulty hard drive that works intermittently. After cold booting, I can
I have a few projects that use boost::shared_ptr or std::shared_ptr extensively (I can convert
Can anyone give me some pointers with regard PHP command execution and best practice?
Where can I find some good pointers on best practices for running ASP.NET MVC
I know this sort of code is not best practice, but nevertheless in certain
im not the best when it comes to SQL still learning the ropes. I
This question is not about 'best' barcode library recommendation, we use various products on

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.