Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am having difficulty passing an array as an argument into int main() with default values. For example:

int main(int a){}

works wonderfully. As does

int main(int a = 1){}

Passing int main() an array also works wonderfully:

int main(int a[3])

However, combining these two concepts seems break:

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

After a significant amount of googleing, I haven't found a solution.

please help me SO, you're my only hope!

EDIT

The purpose of this, in short, is to make my code as little lines as possible, for a challenge my professor recently issued (not for points -- just for learning). The assignment is to create a recursive "12-days-of-chirstmas" program

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 in the array of verses as an argument to main instead of declaring it above the function. I know, not the most memory/stack conscious. But it would eliminate a line :)

like image 517
Michael Jasper Avatar asked Feb 04 '11 02:02

Michael Jasper


1 Answers

This link explains it best:

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

That's why you can declare a function with

void foo (int bar[]);

but you can't declare

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

It has nothing to do with main().

like image 65
chrisaycock Avatar answered Oct 12 '22 03:10

chrisaycock