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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:00:16+00:00 2026-05-20T00:00:16+00:00

I cant figure out where I am messing up. I am passing an array

  • 0

I cant figure out where I am messing up. I am passing an array of character pointers. Inside the function I am trying to use strtok to break up a string into smaller pieces to be assigned to the char * array. I can try printing it off in the function and it all shows up correctly. As soon as I try to print it back in main I just get garbage.

#include <stdio.h>
#include <string.h>

#define CMDLEN 100
#define MAXARG 5
void prompt();
int getCommand (char* cmdAndParameters[]);

int main() {
     int numArgs = 0;
     char* cmdAndParameters [MAXARG];

     while (true){
          prompt ();
          numArgs = getCommand (cmdAndParameters);
     }
}

void prompt() {
     printf("shell> ");
}

int getCommand(char* cmdAndParameters[]){
     int argNum = 0;
     bool foundArg = true;
     char* delimiters = " \t\n";
     char userRequest[CMDLEN];

     fgets(userRequest,sizeof(userRequest), stdin); 

     if ((cmdAndParameters[argNum] = strtok(userRequest, delimiters)) != NULL)
     {  
          argNum++;

          for (; argNum < MAXARG && foundArg; argNum++) {
               if ((cmdAndParameters[argNum] = strtok(NULL,delimiters))
                         == NULL) 
               {
                    foundArg = false;
               }
               // merely to test output remove later
               else {printf("%s\n", cmdAndParameters[argNum]);}
          } 

     }

     return argNum;
}
  • 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-20T00:00:16+00:00Added an answer on May 20, 2026 at 12:00 am

    In this case, your inner array of chars is allocated “automatic”, which is to say, on the stack. When you do the strtok, you’re assigning a pointer to memory allocated on the stack, and then returning — which means the memory is no longer allocated.

    Move the userRequest array into file scope (ie, outside a block) or make the allocation ‘static’ and you’ll have a better shot.

    Update

    Well, it’s a little more than that, now that I look again.

    First of all, you can clean it up considerably if you use a while loop, something like

    argNum = 0;
    while((cmdAndParameters[argNum++] = strtok(userRequest, delimiters)) != NULL)
        ;  /* all the work is happening in the conditional part of the while */
    

    or even a for loop as

    for(argNum = 0; 
        (cmdAndParameters[argNum] = strtok(userRequest, delimiters)) != NULL);
        argNum++)
        ; /* still all the work is in the for */
    

    and now if argNum > 0 you know you found something.

    Second, you need to think about how and when you’re allocating memory. Your cmdAndParameters array is allocated when main starts (on the stack, it’s “automatic”) so it’s around as long as your program, you’re okay there. But your userRequest array is allocated auto in getCommand; when getCommand returns, the memory is deallocated; the stack pointer moves back over it and you have no guarantees any longer. So when you do the strtok, you’re saving pointers into stack, which can lead to no good.

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

Sidebar

Related Questions

i am trying to set the htmlAttribute property but i cant figure out the
I realize this perhaps a naive question but still I cant figure out how
I'm building a questionnaire mvc webapp, and i cant figure out how to pass
I can't figure out a use case for being able to annotate interfaces in
I've spent a good part of the day trying to figure out why a
I've been messing with this site's css FOREVER. I can't figure out how to
I can't figure out why the following wont work, any ideas?? public interface IFieldSimpleItem
I can't figure out how to define the default constructor (when it exists overloads)
I can't figure out if this is a bug in Firefox or an anomaly
I can't figure out how to get the SCOPE_IDENTITY() back to my variables from

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.