0

Hi i want to pass from on to ten arguemnts to a function which will be saved in array.

function( 4, 3, 5); //calling function and passing arguments to it.

void function(int array[10])
{
    cout<<array[0];  // = 4
    cout<<array[1];  // = 3
    cout<<array[2];  // = 5
    cout<<array[3];  // = NULL or 0 or sth else
}

basically I want to have the oportunnity to pass as many arguments as I want to, no more , no less.

It can't be like this.

    function( 4, 3, 5); //calling function and passing arguments to it.

    void function(int x1=NULL , int x2=NULL , int x3=NULL ,int x4=NULL , int x5=NULL)
    {
    for (int i=0 ; i<10;i++)
    {
        array[i] = x1;    // x2 , x3 and so on ...
    }

    cout<<array[0];  // = 4
    cout<<array[1];  // = 3
    cout<<array[2];  // = 5
    cout<<array[3];  // = NULL or 0 or sth else
    }

It's more complicated program than this example, so I NEED it to be array.

  • 2
    Using vector would make the program less complicated – suspectus May 30 '13 at 19:04
  • I'm not sure I see a question here. – John Dibling May 30 '13 at 19:04
  • So you want to pass variable arguments and return arguments in an array? – Shafik Yaghmour May 30 '13 at 19:04
  • I want to pass arguments like they were single ints but i want to function save them to array. If i pass x , a , b to function it saves it into array like array[0]=x , array[1]=b ,array[2]=a – Michał Wesołowski May 30 '13 at 19:11
  • I'm not sure what you're asking for can be done. Like suspectus said, std::vector may be your best bet. I might be misunderstanding your question though, can you try to elaborate a bit more? – Moritz May 30 '13 at 19:14
  • You say it can't be like your example below but to me it seems to behave exactly how you would want it to... – François Moisan May 30 '13 at 19:16
  • I don't have any more ideas how to explain it to you :c – Michał Wesołowski May 30 '13 at 19:17
  • Apparently you're asking for a [variadic function](http://en.cppreference.com/w/cpp/utility/variadic) – perhaps the example `add_nums` given [here](http://en.cppreference.com/w/cpp/utility/variadic/va_start) will help you. But I suspect you're approaching the problem from a rather wrong angle, you should read a good book about C++. For one thing, note that `NULL` isn't a useful value for objects – this is a macro expanding to `0`, really meant for pointers. Don't confuse C++ pointers / objects with e.g. Java references. – leftaroundabout May 30 '13 at 19:33

3 Answers3

0

Why can't you just pass in an array of values and the length of the array? It seems like that would do pretty much what you're asking. Example:

int main{
  int myArray[3] = { 4, 3, 5 };
  function( myArray, 3 );
}

void function( int * argsArray, int argsArrayLength ){
  int i;
  for( i = 0; i < argsArrayLength; i++ )
    cout << argsArray[i] << endl;
}
0

If the arguments you use are constant expressions you could do this:

template <int... Entries>
void function() {
    int array[sizeof...(Entries)] = {Entries...};

    for (int number : array) {
      std::cout << number << ' ';
    }
}

And this is how you use it:

function<4,3,6>(); // prints "4 3 6"
champagniac
  • 444
  • 3
  • 4
0

One approach would be to declare a veridac function as defined in the header cstdarg.

Can't say I've actually used them for anything, but a basic approach to accomplish what you appear to be trying to do would look something like:

#include "stdarg.h"

void myfunction(int argcnt, ...){
  va_list args;
  int myarray[argcnt];

  va_start(args, argcnt);
  for(int i=0;i<argcnt;i++){
    myarray[i] = va_arg(args,int);
  }
  va_end(ap);
  // At this point, myarray[] should hold all of the passed arguments
  // and be ready to do something useful with.
}

In this example, the number of additional arguments to process is passed in the first parameter. So calling:

myfunction(5,1,2,3,4,5);

Would generate a local variable equivalent to myarray[5]={1,2,3,4,5}

The Wikipedia entry for stdarg.h is also a pretty decent resource for this approach. Also, this StackExchange discussion has some really good information on more complex implementations.

Community
  • 1
  • 1
Kent Butler
  • 109
  • 6