在许多C++ IDE和编译器中,当它为您生成主函数时,它看起来像这样:
int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)
当我在没有IDE的情况下编写C++代码时,只需使用命令行编译器,我输入:
int main()
Run Code Online (Sandbox Code Playgroud)
没有任何参数.这意味着什么,对我的计划至关重要?
这是我在学习期间发现的:
#include<iostream>
using namespace std;
int dis(char a[1])
{
int length = strlen(a);
char c = a[2];
return length;
}
int main()
{
char b[4] = "abc";
int c = dis(b);
cout << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以在变量中int dis(char a[1]),[1]似乎什么都不做,根本不起作用
,因为我可以使用a[2].就像int a[]或char *a.我知道数组名称是一个指针,以及如何传达一个数组,所以我的谜题不是这个部分.
我想知道的是为什么编译器允许这种行为(int a[1]).或者它有其他我不知道的含义?
我只是在学习C,并且想知道我应该在我的主要方法中使用哪一个.有什么区别吗?
编辑:那么哪一个更常用?
在我的代码中:
char *str[] = {"forgs", "do", "not", "die"};
printf("%d %d", sizeof(str), sizeof(str[0]));
Run Code Online (Sandbox Code Playgroud)
我得到了输出12 2,所以我怀疑是:
str和str[0]是字符指针,对不对?c ×4
arrays ×2
c++ ×2
pointers ×2
argc ×1
argv ×1
c++-faq ×1
declaration ×1
definition ×1
parameters ×1
sizeof ×1
standards ×1
terminology ×1