开发者

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

开发者 https://www.devze.com 2023-02-08 05:55 出处:网络
I am having difficulty passing an array as an argument into int main() with default values. For example:

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开发者_JAVA百科","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 :)


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().


Addressing the array passing rather than the unlikely use of main(), due to the array really being 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;
}


The main() function should only receive none or two parameters the quantity of command line arguments and the arguments itself.

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

Where argc is the quantity of arguments and argv is one array of C-strings containing the arguments.

EDIT:

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

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();
}


#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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消