0

Possible Duplicate:
Passing Variable Number of Arguments with different type - C++

I have a function that takes n arguments.

And I want to call it like, function_name(number_args - 1, input, args_from_console[], output)

How do I do that?

*the function that takes n arguments already is written and works... I just don't want to hard code the variables being passed in.

Edit: (adding code)

struct fann *ann = fann_create_standard(num_layers, 
                                            Config::NUMBER_OF_INPUT_NEURONS,
                                            Config::WIDTH_IN_BITS, 
                                            Config::WIDTH_IN_BITS, 
                                            Config::NUMBER_OF_OUTPUT_NEURONS);

the function above can have at least 3 arguments... the required arguments are num_layers, num_input, and num_output)

the optional arguments are the hidden layers of the neural network (what they are call isn't important.... but basically... it could look like this:

fann_create_standard(#layers, #input, #hidden1, #hidden2, #hidden3, #hidden4, ... #output);

what I want to be able to do, is pass in command line arguments to change how many layers, and what the values of each of the hidden layers are (the middle arguments in this function call), so that I don't have to re-compile the program every time I want to re-configure the network.

Community
  • 1
  • 1
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 5
    Huh? Could you be more specific, I don't get it.. – Kiril Kirov Apr 06 '11 at 16:58
  • Yes, please clean up this question. – mmocny Apr 06 '11 at 17:00
  • Do you mean like these questions? http://stackoverflow.com/questions/41400/how-to-wrap-a-function-with-variable-length-arguments http://stackoverflow.com/questions/205529/c-c-passing-variable-number-of-arguments-around http://stackoverflow.com/questions/1056411/how-to-pass-variable-number-of-arguments-to-printf-sprintf – AVH Apr 06 '11 at 17:01
  • 1
    You explicitly stated that you are in C++, so I removed the C tag. – Puppy Apr 06 '11 at 17:02
  • Exact duplicate: http://stackoverflow.com/questions/3555583/passing-variable-number-of-arguments-with-different-type-c – David Titarenco Apr 06 '11 at 17:05
  • Added details. And not a duplicate. All the types or ints. – NullVoxPopuli Apr 06 '11 at 17:10
  • NOTE that this question has NOTHING TO DO with variadic functions. – Pete Wilson Apr 06 '11 at 17:36
  • @DavidT, note that the question is not a duplicate. It's not about varargs, it's about passing argv from main( ) – Pete Wilson Apr 06 '11 at 17:44
  • So: this question, if it really was closed as a duplicate, was closed in error. The question does not concern varargs at all, although it's easy to read it that way if one reads it quickly. I had to read it multiples to find out it's about something else entirely, namely, how to pass the command-line argument strings given to main( ) at execution time. So how do we UNclose the question, please? Man, you guys got some QUICK trigger fingers :-) – Pete Wilson Apr 06 '11 at 17:48
  • @Pete: His example call of `fann_create_standard` can only be achieved by varargs. Google it and see http://leenissen.dk/fann/html/files/fann-h.html#fann_create_standard if you really need proof. – Potatoswatter Apr 06 '11 at 17:52
  • You are mistaken. It LOOKS like a varargs problem, no doubt about it. But it's not. It's a question of passing some or all of the command-line arguments given to main( ) on to his called function. And it's not true that fann_create_standard can only be achieved by .... Please take a more careful look. You will see that the function wanted to receive the first four command-line arguments from main( ) and a count of those args (i.e., = 4). This is no longer obvious since he edited his post, but it wqs clear in the first version. – Pete Wilson Apr 06 '11 at 18:05
  • @PS, so restore my downvotes, you .... you potatoswatter :-) – Pete Wilson Apr 06 '11 at 18:06
  • @PotatoSwater let's look at it this way. The OP has made it clear that he cannot modify whatever that function is ... yeah, fann_create_standard( ). Therefore, it cannot be a varargs question because, by the varargs definiton, varargs ONLY OPERATES in the environment of called (and, in this case, unmodifiable) function. So varargs is completely out of the picture wrt this question. – Pete Wilson Apr 06 '11 at 18:14

4 Answers4

0

Hard-code the arguments. It is certainly the best alternative.

If there are really too many, package them into an appropriate data structure and rewrite the function to accept that instead.

Edit: I see that you are using an external library. Fortunately they already provide another function that accepts a data structure instead of a variable number of arguments.

FANN_EXTERNAL struct fann *FANN_API fann_create_standard_array(
    unsigned        int     num_layers,
    const unsigned  int     * layers
)

Simply use this instead.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

I think what you're describing is that you want a variable number of arguments. It's called varargs, and this link should be helpful for you:

http://en.wikipedia.org/wiki/Stdarg.h#varargs.h

Unfortunately I see that your arguments are NOT all the same type. This is a problem. You'll need to use format strings (like printf) so that your code will be able to infer the correct type when it goes to fetch the data from the stack.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
0

Rewrite the function so it doesn't need that. Have it take a vector, instead of an array, then the number of arguments can be retrieved from the size() function.

If I understand you correctly, the function you want to call looks something like this(Ignore the argument types, I'm just using all ints for simplicity's sake)

int func(int n, int input, int args[], int & output);

And you don't want to have to figure out n. Just wrap the function, thusly:

int func_wrap(int input, std::vector<int> & args, int & output)
{
    return func(args.size() - 1, input, &args[0], output);
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
-2

SEE EDIT BELOW!! (I originally thought this had all the earmarks of pure homework)

Just reading your code for sense, could it be that your parameter "args_from_console[]" is really char **argv (or char *argv[]) that was passed to main( ), but presented to you here in disguise? In other words, does the caller of your function look something like this?

int
main ( int argc, char **argv ) {
   ...
   ...
  function_name(number_args - 1, 
    input, 
    argv,          // <-- from the command line, right? 
    output);
    ..
  return 0;
}

If so, then main( ) is giving to your function the tokenized command-line argument string it received when it started. This argument is a pointer to an array of character pointers. The first member of that array points to the first command-line argument; the second member points to the second command-line argument; and so on for each command-line argument that the user typed to start the main( ) program.

Question for you: How can you know how many discrete arguments were passed in to main( ) when it started?

EDIT: I edited this after the OP made his example plainer.

IF you just want to give the command-line arguments to the called function, just pass in both int argc; and char **argv and let him figure it out. Or you modify the called function to figure it out for him.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51