Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

c++ - Defining own main functions arguments argc and argv

i want to create an object of type QApplication which needs the main functions arguments argc and argv as an input:

QApplication app(argc, argv);

Since i am within a user defined function without access to the main function i want to define this arguments on my own. I have tried several approaches but i cannot get the type conversion right. My last approach did not work either:

int argc = 1;
char **argv; 
char arguments[1][12] = {{"cgalExample"}};
argv = arguments;

Thanks for any hint.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quick and dirty, but working for QApplication:

char *argv[] = {"program name", "arg1", "arg2", NULL};
int argc = sizeof(argv) / sizeof(char*) - 1;

For a more complete and C standard conforming solution see D.Shawley's answer.

Why your solution doesn't work is simple:

array[i][j] results in a i*j matrix. But what you actually want is an array with pointers to strings in it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...