定义自己的主要函数参数argc和argv [英] Defining own main functions arguments argc and argv
问题描述
我想创建一个QApplication类型的对象,该对象需要主要函数参数argc和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;
感谢任何提示。
推荐答案
又快又脏,但可用于 QApplication
:
char *argv[] = {"program name", "arg1", "arg2", NULL};
int argc = sizeof(argv) / sizeof(char*) - 1;
有关更完整且符合C标准的解决方案,请参见D。肖利的答案。
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]
生成 i * j
矩阵。但是,您真正想要的是一个数组,其中包含指向字符串的指针。
array[i][j]
results in a i*j
matrix. But what you actually want is an array with pointers to strings in it.
这篇关于定义自己的主要函数参数argc和argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!