简体   繁体   English

警告:“ int main(int,char ***)”的第二个参数应为“ char **” [-Wmain](GNU C ++编译器; Ubuntu 12.10)

[英]warning: second argument of ‘int main(int, char***)’ should be ‘char **’ [-Wmain] (GNU C++ compiler; Ubuntu 12.10)

Trying to clone the "yes" command using C++ as a little experiment (this is on Ubuntu 12.10), and there's a little problem here: 尝试使用C ++克隆“ yes”命令作为一个小实验(这在Ubuntu 12.10上),这里存在一个小问题:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

void yes (char* cmd[]) {
  if ( cmd != NULL ) {
    while (true) {
      cout << cmd[1] << endl;
    }
  } else {
    while (true) {
      cout << "y" << endl;
    }
  }
}

int main(int argc, char** argv[]) {
  yes(argv[1]);
  return 0;
}

If I leave it as is, I get the warning as described in the title. 如果我将其保留原样,则会收到标题中所述的警告。 If I remove one of the asterisks on argv, I get an error about converting "char*" to "char**". 如果删除argv上的星号之一,则会收到有关将“ char *”转换为“ char **”的错误。 And removing the extra function (ie putting it all in main, like so): 并删除额外的功能(例如,将其全部放置在main中):

int main(int argc, char** argv) {
  if ( argv != NULL ) {
    while (true) {
      cout << argv[1] << endl;
    }
  } else {
    while (true) {
      cout << "y" << endl;
    }
  }
  return 0;
}

makes no difference about the warning. 警告没有区别。

Thanks in advanced... 提前致谢...

You can write char **argv or char *argv[] but not both double-star and double-brackets. 您可以编写char **argvchar *argv[]但不能同时写双星号和双括号。

ISO/IEC 14882:2011 §3.6.1 Main function ISO / IEC 14882:2011§3.6.1主要功能

An implementation shall not predefine the main function. 实现不得预定义main功能。 This function shall not be overloaded. 此功能不得重载。 It shall have a return type of type int , but otherwise its type is implementation-defined. 它的返回类型应该是int类型,否则它的类型是实现定义的。 All implementations shall allow both of the following definitions of main: 所有实现均应允许以下两个main定义:

 int main() { /* ... */ } 

and

 int main(int argc, char* argv[]) { /* ... */ } 

In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. 在后一种形式中, argc是从运行程序的环境传递到程序的参数数量。 If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "" . 如果argc为非零,则这些参数必须以argv[0]argv[argc-1]以指针指向以空字符结尾的多字节字符串(NTMBS)(17.5.2.1.4.2)的初始字符,而argv [0]为指向ntmbs初始字符的指针,代表用于调用程序的名称或"" The value of argc shall be non-negative. argc的值应为非负数。 The value of argv[argc] shall be 0. argv[argc]的值应为0。

The 2nd parameter to main is an array of C- Strings, containing the options you pass in, so it should be char*argv[] or char ** . main的第二个参数是C字符串数组,其中包含您传入的选项,因此应为char*argv[]char **

Take a look at the liveworkspace snippet 看看liveworkspace片段

Remove the brackets from argv on main : 取下支架argvmain

int main(int argc, char** argv) {

AND from cmd on yes cmdyes

void yes (char* cmd) {

Seems like the first answer nailed it. 似乎第一个答案已确定。 Long story short, it wasn't the removal of the "yes" function (and subsequent merge of its code into main) OR the removal of one of the asterisks (ie "char* argv" instead of "char** argv"), but a combination of both that removed the warning (simply removing one of the asterisks with the function still in place caused a conversion error). 长话短说,这不是删除“ yes”函数(以及随后将其代码合并到main中)或不是删除一个星号(即“ char * argv”而不是“ char ** argv”) ,但两者的组合均消除了警告(仅删除其中一个星号且功能仍在起作用会导致转换错误)。

Thanks again! 再次感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM