Pass an array directly to a function?

Hello guys,

I'm wondering, is it possible to pass an array to a function directly using C++0x? this what they call initialiser list. I don't get the idea. I'm looking for something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double sumFunc(double[] list)
{
   i = 0;
   double sum = 0;
   while(list[i] != '\0')
   {
      sum += list[i];
   }
   return sum;
}

int main()
{
   cout << sumFunc({1,2,3,4,5}) << endl;
}


So the idea is, I want to pass the array directly without initialising it before the call. Is this possible with this way or any other way?

Thank you :)
I don't know much about this particular rule in c++0x, but I don't find it hard to understand. C++ standard makers are trying to make C++ more flexible, syntax-wise, so that special needs are met.

Let me ask you a question:
Which line of code is the first line of sunFunc()?


The answer is "i=0". It's not "double sumFunc(double[] list)". Function name is just a function pointer. The actual first line of code is "i=0".


Okay, now, in your word "I want to pass the array directly without initialising it before the call".
That means, you want the array to be initialized before "i=0" is executed.

Will the code do what you want?
Yes, because the array is initialized when the execution falls on "cout << sumFunc({1,2,3,4,5}) << endl;". It's apparently one step before "i=0" is executed.
Thank you for your answer :)

But this code doesn't compile, does it? I want a working example of that!
closed account (zwA4jE8b)
http://cplusplus.com/doc/tutorial/arrays/

The last part has an example of passing an array to a function.
Watch that while loop on Line 5, you may not have pasted the code but I don't see where that condition is ever met. Also have you done any work with the operator "new"? http://www.cplusplus.com/reference/std/new/
You can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <initializer_list>

double sumFunc(std::initializer_list<double> list)
{
   double sum = 0.0;
   for(auto i: list) sum += i;
   return sum;
}

int main()
{
   std::cout << sumFunc({1,2,3,4,5}) << std::endl;
}
Last edited on
Thank you Galik, this is exactly what I was looking for. But your code still gives me an error at the auto part:

main.cpp:7:14: error: expected initializer before ':' token

How is this to be fixed?

Thank you!
@TheDestroyer

You have to use a compiler that supports it. I use GCC v 4.6. Earlier versions do not support that feature of c++0x.

You can use this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <initializer_list>

double sumFunc(std::initializer_list<double> list)
{
   double sum = 0.0;
   for(auto i = list.begin(); i != list.end(); ++i) sum += *i;
   return sum;
}

int main()
{
   std::cout << sumFunc({1,2,3,4,5}) << std::endl;
}

If your compiler doesn't support the auto keyword then you will need this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <initializer_list>

double sumFunc(std::initializer_list<double> list)
{
   double sum = 0.0;
   std::initializer_list<double>::const_iterator i;
   for(i = list.begin(); i != list.end(); ++i) sum += *i;
   return sum;
}

int main()
{
   std::cout << sumFunc({1,2,3,4,5}) << std::endl;
}
Last edited on
Thank you so much bud! I got it :) I'm grateful!!
Topic archived. No new replies allowed.