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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:58:31+00:00 2026-05-22T18:58:31+00:00

I need to return a char** but when I try to do this, the

  • 0

I need to return a char** but when I try to do this, the compiler tells me that I want to return the address of a local variable. How can I do that? I know that I should allocate space for this variable but how? Here is my code, but the second printf doesn’t appear and the function returns nothing:

char** parse_cmd(const char* cmdline) {
char** arguments = (char**)malloc(100);
int i;
int j=0, k=0;
printf("%s\n", cmdline);

for(i=0; i<100; i++) {
    arguments[i] = malloc(100);
}

for(i = 0; i < strlen(cmdline); i ++) {
    if(cmdline[i] != ' ') {
        arguments[j][k] = cmdline[i];
        k++;
    } else {
        arguments[j][k] = '\0';
        j++;
        k = 0;
    }
}

printf("%s\n", arguments[1]);

return arguments;
}
  • 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-22T18:58:32+00:00Added an answer on May 22, 2026 at 6:58 pm

    Here’s the code I assembled – and tested. It uses dynamic memory allocation for both the argv argument list and for each argument as it is assembled. The function release_cmd() releases the allocated space. The function cleanup() is internal and releases allocated space on a failure, before returning a null double-pointer. This simplifies the error handling. There’s a minimal test harness in the prompt() function and main(). I haven’t run in under valgrind, but the MacOS X implementation of malloc() quite often spots problems so I’m moderately confident there’s no gross memory abuse – but there could be an off-by-one leak. I haven’t tested the cleanup() code by faking an allocation failure.

    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void release_cmd(char **argv)
    {
        for (size_t i = 0; argv[i] != 0; i++)
            free(argv[i]);
        free(argv);
    }
    
    static char **cleanup(size_t argc, char **argv)
    {
        argv[argc] = 0;
        release_cmd(argv);
        return 0;
    }
    
    char **parse_cmd(const char* cmdline)
    {
        size_t argc = 2;
        char **argv = malloc(argc * sizeof(char *));
    
        if (argv == 0)
            return 0;
    
        size_t j = 0;  // Index into argv
        size_t len = strlen(cmdline);
    
        for (size_t i = 0; i < len; i++)
        {
            while (isspace(cmdline[i]))
                i++;
            if (cmdline[i] == '\0')
                break;
            if (j > argc - 2)
            {
                size_t newc = (argc * 2);
                char **newv = realloc(argv, newc * sizeof(char *));
                if (newv == 0)
                    return cleanup(argc, argv);
                argv = newv;
                argc = newc;
            }
            size_t argl = 2;    // Length of argument string
            argv[j] = malloc(argl);
            size_t k = 0;       // Index into argv[j]
            while (cmdline[i] != '\0' && !isspace(cmdline[i]))
            {
                if (k > argl - 2)
                {
                    size_t newl = argl * 2;
                    char  *news = realloc(argv[j], newl);
                    if (news == 0)
                        return cleanup(argc, argv);
                    argv[j] = news;
                    argl    = newl;
                }
                argv[j][k++] = cmdline[i++];
            }
            argv[j][k] = '\0';
            argv[j] = realloc(argv[j], k+1);    // Shrink to fit!
            j++;
        }
        argv[j] = 0;
        argv = realloc(argv, (j+1)*sizeof(*argv));  // Shrink to fit!
    
        return argv;
    }
    
    static int prompt(const char *prompt, char *buffer, size_t bufsiz)
    {
        printf("%s", prompt);
        return (fgets(buffer, bufsiz, stdin) != 0);
    }
    
    int main(void)
    {
        char line[1024];
    
        while (prompt("cmd? ", line, sizeof(line)) != 0)
        {
            char **argv = parse_cmd(line);
            char **args = argv;
            while (*args)
                puts(*args++);
            release_cmd(argv);
        }
        putchar('\n');
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in delphi7 i have a function that i need to return a array as
Here is my scenario. For the example lets say that I need to return
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
I need to know how to return a default row if no rows exist
For a current project I am working I need to return an aggregate report
I have a bit of PHP code which I need to return an even
I usually do something like the example below when I need to return error
Working with the following code, I need to return only records where the `point'
I need a function to return a suffix for days when displaying text like
I need a method to return a random string in the format: Letter Number

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.