How to create a variable that is char *[] so it can be passed to a function


How to create a variable that is char * so it can be passed to a function



I'm trying to pass an argument to a function that takes a char * as input:


char *


int nArgs = 0;
CommandLineToArgvW(GetCommandLineW(), &nArgs);
const int commandLineArgsBufferSize = 500;
char commandLineArgs[commandLineArgsBufferSize];
wcstombs(commandLineArgs, GetCommandLineW(), commandLineArgsBufferSize);
int result = Catch::Session().run(nArgs, &commandLineArgs);



Here is the function prototype.


int Session::run( int argc, char* argv );



I am getting a compiler error:



C2664 'int Catch::Session::run(void)': cannot convert argument 2 from 'char (*)[500]' to 'char *'





You have created a pointer to a char array, you need an array of pointers to chars
– vu1p3n0x
Jul 2 at 19:38





Your function takes an array of char*, but you pass a pointer to an array of char
– Olivier Sohn
Jul 2 at 19:39


char*


char




1 Answer
1



The function is presumably intended to take the parameters of main() as arguments. So you want something like:


int main( int argc, char * argv ) {
int rv = Session::run( argc, argv );
}



If you wanted to construct the arguments yourself, something like:


void f() {
char * argv = {"foo", "bar" };
int rv = Session::run( 2, argv );
}





Would something like this work. const int commandLineArgsBufferSize = 2^10; char commandLineArgs[commandLineArgsBufferSize]; wcstombs(commandLineArgs, GetCommandLineW(), commandLineArgsBufferSize); char * commandLineArgsToPass = { commandLineArgs }; int result = Catch::Session().run(nArgs, commandLineArgsToPass);
– Donald Herman
Jul 2 at 23:07






@Donald Nope - that makes no sense. Do you know what the ^ operator does? You seem to be way over-thinking this.
– Neil Butterworth
Jul 2 at 23:13


^






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages