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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:25:56+00:00 2026-06-11T15:25:56+00:00

Below is a function for getting the next token in a program that does

  • 0

Below is a function for getting the next token in a program that does tokenizing. It currently is working but I’m still not sure if it’s correct for what my professor is asking for. If you look in the top comment section he says “Space for the return token should be dynamically allocated.”

Whenever I hear that I expect that I would have to use malloc but I’ve done it without using malloc. Am I correct to think that?

/*
 * TKGetNextToken returns the next token from the token stream as a
 * character string.  Space for the returned token should be dynamically
 * allocated.  The caller is responsible for freeing the space once it is
 * no longer needed.
 *
 * If the function succeeds, it returns a C string (delimited by '\0')
 * containing the token.  Else it returns 0.
 *
 * You need to fill in this function as part of your implementation.
 */
char *TKGetNextToken(TokenizerT *tk) {
char *sPtr, *tPtr, *delim, *temp = NULL, *ret = NULL;

sPtr = tk->sepr; //pointer to the separators

tPtr = tk->ts;
temp = tPtr;

while (tPtr[0] != '\0') //Scan tokenstream
{
    delim = tk->sepr;
    while (delim[0] != '\0') //scan separator stream 
    {
        if (*tPtr == *delim) //Matched with a separator
        {
            if (tPtr == temp) //Check if beginning of the tokenstream
            {
                //then skip over this character.
                temp++;
                break; //Break loop because it may skip checking a char with a prev delim.
            }
            else
            {
                //Cut off current position with null character and pass over it.
                *tPtr = '\0';
                tPtr++;

                if((ret = malloc(sizeof temp) + 1 * sizeof(char)) != NULL) //add 1 for null character '\0'
                    strcpy(ret, temp);

                tk->ts = tPtr; //In position for next token.

                return ret;
            }
        }
        else
        {
            delim++; //Go to next separator.
        }

    }

    tPtr++; //Go to next character.
    count++;
}

if((ret = malloc(sizeof temp) + 1 * sizeof(char)) != NULL) //add 1 for null character '\0'
    strcpy(ret, temp);

tk->ts = tPtr;
return ret;
}

EDIT: So I think I fixed it but curious, using malloc the way I did, doesn’t that only allocate space for the pointer address and not the entire token?

  • 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-11T15:25:58+00:00Added an answer on June 11, 2026 at 3:25 pm

    That is a possible implementation, but not the one your professor is asking for. Your professor wants you to allocate memory and copy the token into it, then return a pointer to that allocated memory.

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

Sidebar

Related Questions

i want to implement next previous functionality using below function. - (void)motionEnded:(UIEventSubtype)mt withEvent:(UIEvent *)event
In the below code I am getting the following error on line If (Not
Below is the function that I am using in jquery function addlnkfieldmkAndyr() { var
I'm trying to use the function regmatches, in R, but i'm getting an error
I have a form and 2 javascript functions below but I am getting 2
When I put the below function into leon repl , I get java.lang.IllegalArgumentException: Wrong
I am using the below function of javascript to enable and disable the radio
Anyone experienced exception using below function? tdse.GetObject(tmpFolderWebDavURL, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadAll) as Folder; Seems if
I would like the below function to be more flexible and accept multiple callbacks
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment

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.