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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:29:53+00:00 2026-05-13T10:29:53+00:00

I’m having trouble figuring out how to pass strings back through the parameters of

  • 0

I’m having trouble figuring out how to pass strings back through the parameters of a function. I’m new to programming, so I imagine this this probably a beginner question. Any help you could give would be most appreciated. This code seg faults, and I’m not sure why, but I’m providing my code to show what I have so far.

I have made this a community wiki, so feel free to edit.

P.S. This is not homework.

This is the original version

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

void fn(char *baz, char *foo, char *bar) {
     char *pch;

     /* this is the part I'm having trouble with */
     
     pch = strtok (baz, ":");
     foo = malloc(strlen(pch));
     strcpy(foo, pch);

     pch = strtok (NULL, ":");
     bar = malloc(strlen(pch));
     strcpy(bar, pch);

     return;
}

int main(void) {
     char *mybaz, *myfoo, *mybar;

     mybaz = "hello:world";

     fn(mybaz, myfoo, mybar);

     fprintf(stderr, "%s %s", myfoo, mybar);
}

UPDATE Here’s an updated version with some of the suggestions implemented:

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

#define MAXLINE         1024

void fn(char *baz, char **foo, char **bar) {
     char line[MAXLINE];
     char *pch;

     strcpy(line, baz);

     pch = strtok (line, ":");
     *foo = (char *)malloc(strlen(pch)+1);
     (*foo)[strlen(pch)] = '\n';
     strcpy(*foo, pch);

     pch = strtok (NULL, ":");
     *bar = (char *)malloc(strlen(pch)+1);
     (*bar)[strlen(pch)] = '\n';
     strcpy(*bar, pch);

     return;
}

int main(void) {
     char *mybaz, *myfoo, *mybar;

     mybaz = "hello:world";

     fn(mybaz, &myfoo, &mybar);

     fprintf(stderr, "%s %s", myfoo, mybar);

     free(myfoo);
     free(mybar);
}
  • 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-13T10:29:53+00:00Added an answer on May 13, 2026 at 10:29 am

    First thing, those mallocs should be for strlen(whatever)+1 bytes. C strings have a 0 character to indicate the end, called the NUL terminator, and it isn’t included in the length measured by strlen.

    Next thing, strtok modifies the string you’re searching. You are passing it a pointer to a string which you’re not allowed to modify (you can’t modify literal strings). That could be the cause of the segfault. So instead of using a pointer to the non-modifiable string literal, you could copy it to your own, modifiable buffer, like this:

    char mybaz[] = "hello:world";
    

    What this does is put a size 12 char array on the stack, and copy the bytes of the string literal into that array. It works because the compiler knows, at compile time, how long the string is, and can make space accordingly. This saves using malloc for that particular copy.

    The problem you have with references is that you’re currently passing the value of mybaz, myfoo, and mybar into your function. You can’t modify the caller’s variables unless you pass a pointer to myfoo and mybar. Since myfoo is a char*, a pointer to it is a char**:

    void
    fn(char *baz, char **foo, char **bar) // take pointers-to-pointers
    
    *foo = malloc(...);  // set the value pointed to by foo
    
    fn(mybaz, &myfoo, &mybar);  // pass pointers to myfoo and mybar
    

    Modifying foo in the function in your code has absolutely no effect on myfoo. myfoo is uninitialised, so if neither of the first two things is causing it, the segfault is most likely occurring when you come to print using that uninitialised pointer.

    Once you’ve got it basically working, you might want to add some error-handling. strtok can return NULL if it doesn’t find the separator it’s looking for, and you can’t call strlen with NULL. malloc can return NULL if there isn’t enough memory, and you can’t call strcpy with NULL either.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to do more than just call InsertOnSubmit(). You… May 14, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer with the namespace added you can do this: $opensearch =… May 14, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer Neeeeeeevermind. I had overlooked the [TableView reloadData] method to refresh… May 14, 2026 at 7:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.