Reputation:
I'd like to be able to save argv into a struct so that it can be passed to functions like so:
struct Parameters {
int argc;
char * argv[];
};
void Start(
Parameters P
) {
};
int main (
int argc,
char * argv []
) {
Parameters P;
P.argc = argc;
P.argv = & argv;
return 0;
}
But with:
clang++ -std=c++2a -stdlib=libc++ -rtlib=compiler-rt -Ofast Start.cpp -o Start && ./Start;
I'm getting this error:
Start.cpp:21:9: error: array type 'char *[]' is not assignable
Is there a way of saving argv to a variable? Any help would be very much appreciated.
Upvotes: 0
Views: 1178
Reputation: 264461
A simple way is to convert it to a vector of strings:
int main(int argc, char* argv[])
{
// Note: use argv + 1 to skip the application name in args.
// If you want to include the application name then don't use
// the +1
std::vector<std::string> args(argv + 1, argv + argc);
// Now this can be passed to functions easily.
// args.size() == number of arguments.
// args[x] == the string for argument x
}
Upvotes: 2
Reputation: 11410
You can simply change to:
struct Parameters
{
int argc;
char ** argv;
};
Your argv
array of pointers to char
will decay to a pointer to pointer to char
.
Then, your main becomes simpler, with:
P.argv = argv;
Upvotes: 2