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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:48:26+00:00 2026-05-28T07:48:26+00:00

What I want my program to do is to read an input text from

  • 0

What I want my program to do is to read an input text from the user, tokenize that string with the whitespace being the delimiter and store each of the tokens int an array of char* which will then be returned.

here is a snippet of code that I am trying to make it work correctly:

typedef char* String;
String* split(char* cmd)
{
    char* param;
    char tmp[128];
    String* result = (String*) malloc(10*sizeof(String));
    memset(result,NULL,10);

    strcpy(tmp,cmd);

    param = strtok(tmp," ");

    int index = 0;
    while(param && index < sizeof(result) / sizeof(*result))
    {

        result[index] = (char*) malloc(strlen(param));
        strcpy(result[index],param);

        param = strtok(NULL," ");
        index++;
    }

}

Where cmd is the string that I am tokenizing and result is the array that will contain each token.

This snippet causes errors when trying to iterate through the returned result using a simple for loop (A segmentation fault arises)

String* splittedCmd = split(command);

int i;
for(i=0;i<10;i++)
{
    if(splittedCmd[i] != NULL)
        printf("%s\n",splittedCmd[i]);
}
  • 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-28T07:48:26+00:00Added an answer on May 28, 2026 at 7:48 am

    There are several things wrong here.

    First and most obvious, you are returning result which is an array (but decays to a pointer to an array) that is allocated on the stack in the function so it is reclaimed when the function returns. You need to dynamically allocate the array (and rely on the caller to free it):

    String *result = malloc(10 * sizeof(String));
    

    Also, your condition to stop the while loop:

    if(index == sizeof(result))
    

    Will let the loop go until index is 40 (if char* is 4 bytes on your platform) because sizeof returns the size of the operand in bytes, not array elements, so sizeof(result) is (again, platform dependent) 40. This is obviously beyond the bounds of the array.

    If you were still using a local array instead of malloc, you could change that to

    if (index == sizeof(result) / sizeof(*result))
    

    However, you can’t do that now because result is only a pointer instead of an array and sizeof(result) will always be the size of a pointer on your platform.

    You can just remove that if completely and change the while condition to

    while (param && index < 10)
    

    Which makes sure that param is not NULL and also that index is less than 10. You should consider making a #define or a const int or something for the size of the array and use that instead of using a magic number.

    You also need to change

    memset(result,NULL,10);
    

    to

    memset(result,NULL, sizeof(String) * 10);
    

    Because if you don’t, memset is only setting the first 10 bytes of the memory pointed to be result to 0, instead of the whole thing, because it takes the number in bytes, not array elements.

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

Sidebar

Related Questions

I want to read input from user using C program. I don't want to
I want to make a program that will read some number in string format
Ok, so I'm making a program that will read from a text file, convert
I am supposed to write a program that should read from input numbers in
i want to read 2 values from user input using lisp. I want to
I want to run the following command from a C program to read the
I want to write a program in C/C++ that will dynamically read a web
Okay, so ultimately I want a program where the user can input simple data
My program reads a list of integers from user input [ keyboard] and calculates
I have to write a program that read from a file that contains 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.