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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:49:51+00:00 2026-06-03T03:49:51+00:00

I have a C++ function to which I have to pass char* arguments[] .

  • 0

I have a C++ function to which I have to pass char* arguments[]. The prototype of this function is as follows:
void somefunc(char* arguments[])

To pass the above array to this function, I am generating this array from another function which can return me this char* array output, and this looks somehwhat like this.

char** genArgs(int numOfArgs,...)
{
    va_list argList;    
    char** toRet = new char*[numOfArgs];
    va_start (arguments, numOfArgs);
    for(int cnt = 0; cnt < numOfArgs ; ++cnt)
        toRet[cnt] = va_arg(argList, char*);

    va_end(arguments);
}

I am confused how to actually write the above function correctly, given that I will be passing the inputs to function something like : genArgs(3, a.c_str(), b.c_str(), c.c_str()) or genArgs(2, a.c_str(), b.c_str())
How can I generate the above char* array without using char** (since this would have to be deleted before returning from the function to avoid memory leak).

  • 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-03T03:49:52+00:00Added an answer on June 3, 2026 at 3:49 am

    From your declaration of genArgs, it seems that whenever you call genArgs, you know how many arguments you want to pass. Is this correct? If so (and if you are using C++11) then you can use std::array in the calling function. So instead of this:

    char** arglist = genArgs (4, "MyArg1", mYaRG2", "LastArg", 0) ;
    somefunc (arglist) ;
    delete[] arglist ;
    

    you can do it this way:

    #include <array>
    std::array<char*, 4> arglist { "MyArg1", mYaRG2", "LastArg", 0 } ;
    somefunc (&arglist[0]) ;
    

    But there is really nothing wrong with the first solution, unless you are a C++ ascetic. (However, you do need a return toRet ; statement in genArgs!)

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

Sidebar

Related Questions

I have around 8-9 parameters to pass in a function which returns an array.
I have an overloaded function which I want to pass along wrapped in a
I have this function which removes occurrences of a given element within the list
I have a function which gets a key from the user and generates a
I have a char array. I take its address and pass it to a
I have this C function which attempts to tell me if a sub string
I have a Python function which takes several arguments. Some of these arguments could
I have a function which requires me to pass a UTF-8 string pointed by
I have a templated function which I want to specialize foo to const char[N]
Say I have a C function which takes a variable number of arguments: How

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.