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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:14:48+00:00 2026-06-01T19:14:48+00:00

Hi guys I got some problems with using a function with a reference to

  • 0

Hi guys I got some problems with using a function with a reference to a string as argument. I read that you should use e double pointer for this, but I can’t get it to work.
This is (partly) my code.

enum errCode { ERR_NONE = 0, ERR_EMPTY, ERR_FULL, ERR_MEM, ERR_INIT, ERR_COMMAND, ERR_UNDEFINED };
typedef enum errCode ErrCode;

typedef enum {
    no = 0, add, del, src, show, exit
} Command;

int main(void) {
    char stringval[50];
    char stringval2[50];
    ErrCode err;
    Command currentCommand = no;

    printf("Enter a command\n");

    if (fgets(stringval, 50, stdin) != NULL) {
        char *p;
        if ((p = strchr(stringval, '\n')) != NULL)
            *p = '\0';
    }

    ErrHandler(
            extractCommand(&currentCommand, stringval, &stringval2)
            );

    printf("stringval 2 = %s.\n", stringval2);

    return 0;
}

ErrCode extractCommand(Command *command, char *inputString, char **outputString) {

    char *strTemp;
    char *strTemp2;

    //Get the first word of the string
    strTemp = strtok(inputString, " ");

    strTemp2 = strtok(NULL, " ");
    *outputString = strTemp2;

    //Check if it equals a command
    if (strcmp(strTemp, "exit") == 0) {
        *command = exit;
        return ERR_NONE;
    } else if (strcmp(strTemp, "add") == 0) {
        *command = add;
        return ERR_NONE;
    } else if (strcmp(strTemp, "del") == 0) {
        *command = del;
        return ERR_NONE;
    } else if (strcmp(strTemp, "src") == 0) {
        *command = src;
        return ERR_NONE;
    } else if (strcmp(strTemp, "show") == 0) {
        *command = show;
        return ERR_NONE;
    } else {
        *command = no;
        printf("%s", strTemp);
        return ERR_COMMAND;
    }
}

This is what my output looks like:

Enter a command
add this is a test
stringval 2 = z˜ˇøÀo‡èK‡èT¯ˇø.

I obviously want to have the second word of the inputted string, but I’m doing something wrong.
Thx for the help!

  • 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-01T19:14:49+00:00Added an answer on June 1, 2026 at 7:14 pm

    stringVal2 is not initialised and is never populated: that is reason junk is being printed. There is no need in this case to pass a char**, passing a char* will work. However, this:

    outputString = strTemp2;
    

    does not copy the content of strTemp2 to outputString: it makes outputString point to the same address as strTemp2: use strcpy().


    A double pointer, char** for example, is commonly passed to a function when that function allocates a new buffer for the argument (which is not the case in the posted code):

    char* buf = NULL;
    my_alloc(&buf);
    
    void my_alloc(char** p)
    {
        *p = malloc(10);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got some basic problem again. I need to modify a function that previously returned
Ok guys. What I'm trying to do: I've got some backend code written in
Okay guys, I've got one thats got me baffled. I've done some searching for
Here's the situation: I've got some lengthy non-UI code that needs to be run
Hey Guys I would very much appreciate some help with the following. We're using
hey guys, I got some difficulty here. It is purely coding algorithm problem. Okay,
Hey guys got a quick question here, how do i set up TweetStream after
Hey guys I've got a whole lot of HTML to format with myString +
Have you guys and gals got any tips or hacks for making the most
Hi guys i previously asked a question and got a good solution. here is

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.