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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:00:33+00:00 2026-06-11T05:00:33+00:00

I want to break a sentence into words using pointers. I wrote some code

  • 0

I want to break a sentence into words using pointers.

I wrote some code but I am really confused because I get segmentation fault.
kindly help me out.
Thanks in advance.

char **breakstring(char *str)
{
    char **temp=(char **)malloc(5*sizeof(char *));
    char **temp_store = temp;
    while((*str) != '/0')
    {
      while((*str != '\0') && *str!=' ')
      {
      **temp=*str;
      **temp++;
      *str++;
      }

      str++;
      temp++;
    }
    return temp_store;
}

int main()
{
    char **arra;
    char *arr="this is a stupid string";
    arra=breakstring(arr);
    return 0;
}
  • 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-11T05:00:35+00:00Added an answer on June 11, 2026 at 5:00 am
    char **temp=(char **)malloc(2*sizeof(char *));
    

    You allocated 2 pointers. Your input string has more than 2 spaces. If you still want to use dynamic memory, you’ll have to periodically call realloc to ask for more space, or possibly introduce some other arbitrary maximum that’s greater than 2.

      while(*str!=' ')
      {
    **temp=*str;
    **temp++;
    *str++;
      }
    

    What this says is “while the current char of the string is not a space, add the string to temp, and advance. So if you’re called with "This is a ..." you’ll end up with "This is a ...", "his is a ...", "is is a ...", etc. Inside this loop you want to advance str without assigning to temp. (You’ll also want to add the condition *str, i.e. while (*str && *str != ' ')) Outside the loop you’ll want something like *temp++ = str;, then skip all the space characters. (You might also want to add a NUL terminator to stop the string, but you’ve called the function with a const char * string literal, so to maintain that you’d probably have to copy the whole string onto the heap.)

    All in all, the amount of heap allocations we’re leading into here is somewhat un-C-like for string manipulation. I suggest instead of returning a heap allocation and passing constant strings, you look into an interface like strsep() (the better version of the ugly strtok()), which modifies the caller’s buffer.

    Update: @Rohan is also right about the return value issue. If you keep doing temp++ you need to remember the original position of the start of the allocation, and return that. Although, I would re-iterate looking into strsep or strtok_r.

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

Sidebar

Related Questions

I want to break a sentence apart into words and end marks (assume all
I want to break a string into chunks of 200 characters or smaller, but
I want to break my string using javascript which is fetching some data from
I want user to enter a sentence then I break up that sentence into
i just want to break a large image into some equal parts like 4,
I want to display a very large text file but want to break it
What's the syntax for the aspx pages if you want to break down really
I want to force the Chrome debugger to break on a line via code
I want to break a Python string into its characters. sequenceOfAlphabets = list( string.uppercase
I want to break down a sentence and store each string in an array.

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.