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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:47:08+00:00 2026-06-14T23:47:08+00:00

I’m writing a simple shell program in C. As part of the program, I’m

  • 0

I’m writing a simple shell program in C. As part of the program, I’m writing a tokenizer to break up user input into tokens. Each time the tokenizer is called, it returns the next token. The code for the tokenizer looks like this:

char* nextToken(char string[])
{
char token[50]= {}; //stores the current token
//More code here, will post if necessary
puts(token);
return token;
}

int main()
{
char inputString[] = "cpgm one two <three| script a b >file";
char *token = nextToken(inputString);
while(*token!='\0') //I'm using a char[] with a single null character as a delimiter to indicate the last token has been reached
{
    token = nextToken(inputString);
}

}

When I run the program like this, the “puts” works properly; that is to say, each time the function is called, the appropriate token is printed in the correct order, like this:

cpgm
one
two
<
three
|
script
a
b
>
file

However, when I remove the “puts” line in nextToken and put it in the main function, like this:

 while(*token!='\0')
 {
     puts(token);
     token = nextToken(inputString);
 }

and try to print the token from my main function instead, all I see is a list of weird characters, like this:

�bHd?
�bH
�bH
�
�bHd
�
�bHd?

�
�
�
�
�bHd?

Any idea why this is happening?

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-06-14T23:47:10+00:00Added an answer on June 14, 2026 at 11:47 pm

    You are allocating your string token on the stack. This means that when you return it, it will be deallocated, and can’t be used any more. You should allocate memory for your string instead:

    char *token = malloc(50);
    

    You also then have to remember to free the string later, once you’re done with it, by calling

    free(token);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.