Passing an array as a parameter with default values ​​to int main ()

I find it difficult to pass an array as an argument to int main() with default values. For example:

 int main(int a){} 

works wonderfully. Like

 int main(int a = 1){} 

Passing an int main() array also works just fine:

 int main(int a[3]) 

However, the combination of these two concepts seems broken:

 int main(int a[1] = {0,1}) 

After a significant amount of googleing, I did not find a solution.

Please help me, SO, you are my only hope!

EDIT

The purpose of this, in short, is to make my code as few lines as possible for the task that my professor recently published (not for glasses - just for training). Purpose - to create a recursive program "12 days of the day"

This is my current program.

 #include <iostream> #include <string> void p(std::string v){std::cout<<v;} std::string v[13] = {"A Partridge in a Pear Tree.\n\n","2 Turtle Doves\n","3 French Hens\n","4 Colly Birds\n","5 Gold Rings\n","6 Geese-a-Laying\n","7 Swans-a-Swimming\n","8 Maids-a-Milking\n","9 Ladies Dancing\n","10 Lords-a-Leaping\n","11 Pipers Piping\n","12 Drummers Drumming\n",""}; int main(){ switch(v[12].length()){ case 12:system("pause"); return 0; case 11:p(v[11]); case 10:p(v[10]); case 9: p(v[9]); case 8: p(v[8]); case 7: p(v[7]); case 6: p(v[6]); case 5: p(v[5]); case 4: p(v[4]); case 3: p(v[3]); case 2: p(v[2]); case 1: p(v[1]); case 0: p(v[0]); }v[12] += "0"; main(); } 

I would like to pass an array of verses as an argument to main, rather than declaring it over a function. I know this is not the most memorable / stack. But this will eliminate the line :)

+6
c ++ parameters default-value
source share
4 answers

This link explains this best of all:

In C ++, it is impossible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address.

This is why you can declare a function with

 void foo (int bar[]); 

but you cannot declare

 void foo (int bar[] = {0 ,1}); 

This has nothing to do with main() .

+12
source share

Array addressing, and not the unlikely use of main (), because the array is really passed as a pointer, you can do it like this:

 int defaultInit[2] = {0,1}; // {0,1} means 2 elements for the array. int f(int arg[2] = defaultInit) { return 0; } 
+7
source share

The main() function should take only two or two parameters: the number of command line arguments and the arguments themselves.

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

Where argc is the number of arguments, and argv is one array of C-lines containing the arguments.

EDIT:

He changed focus. You can reduce the number of lines:

 void loop(int i) { if (i < 0) { v[12] += "0"; return; } p(v[i]); loop(i-1); } int main() { if (v[12].size() == 12) return; loop(v[12].size()); main(); } 
+3
source share
 #include <stdio.h> int go(int); int main( int argc, char*argv[]) { go(0); } int go(int argc) { const char* lyrics[] = {...}; printf("On the %d%s day of Christmas\nmy true love gave to me\n",argc, argc==1?"st":argc==2?"nd":argc==3?"rd":"th" ); for(int i=argc-1;i>0;--i) { printf("%d %s\n",i+1,lyrics[i]); } printf("%s\n\n",lyrics[0]); if( argc < 12 ) go(argc+1); return 0; } 
+1
source share

All Articles