By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Are Anonymous arrays possible

P: n/a

Suppose I have a function that expects an array as argument
{
.....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

{
.....
someFunc({1,2,3,4});
......
}
Thanks
Ram
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Ramprasad A Padmanabhan:

Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like

{
....
someFunc({1,2,3,4});
.....
}


Well, there are three issues:

* Avoiding an _automatic_ (local variable) array.

* Specifying the values of a conceptual array in a function call.

* Efficiency of such a specification.

Regarding how to avoid a local variable, initialized each time, if
concurrency is not an issue you can declare the array static:

static int const d[] = {1, 2, 3, 4};
somefunc( d );

Regarding how to specify the values of a conceptual array in a function
call C++ doesn't have a notation for that, so it reduces to the general
problem of specifying an arbitrary number of arguments. The usual
solution, e.g. employed by std::cout "<<", is to use a member operator
or function that returns a reference to the object it's called on. Like
(disregarding issues related to deriving from a class with no virtual
constructor -- you might want to use encapsulation instead)

class Array: public std::vector<int>
{
public:
Array& operator<<( int x ){ push_back( x ); return *this; }
};

inline Array const& temporary( Array const& a ){ return a; }

...
d( temporary( Array() << 1 << 2 << 3 << 4 ) );

Possibly there is such a thing in Boost, <url: http://www.boost.org>, I
think I saw something like that.

Regarding efficiency, first make correct, then check if good enough,
then if it isn't, measure (unless obvious), and only then optimize. :-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
Ramprasad A Padmanabhan wrote:

Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like


The thing that doesn't exist that you are asking for is an "array literal"
which other than for the case of char strings, don't exist in the language.
Jul 23 '05 #3

P: n/a
Ramprasad A Padmanabhan schrieb:

Suppose I have a function that expects an array as argument
{
....
int d[] = { 1,2,3,4}
someFunc(d);
}

I have to create a temporary array d[] and pass 'd' to the function.
Can I avoid the the temporary array
something like
There is no temporary array. There is only one array (d).
If you call type someFunc(d); then the functionheader has to have a
pointer to the array d ( type someFunc(int *myarray) ). Example:
#include <iostream>
using namespace std;
void add_one (int *anarray, int size) { //increments each element
for (int tmp=0; tmp<size; tmp++) anarray[tmp]++;
}

int main () {

int test [4] = {33,22,11,55};
int elements = sizeof(test)/sizeof(test[0]); //not known within function

add_one (test,elements);

for (int tmp=0; tmp<elements; tmp++)
cout << "Element No." << tmp << " : " << test[tmp] << endl;

return 0;
}


{
....
someFunc({1,2,3,4});
.....
}

If you use something like "type someFunc({1,2,3,4});"

then this would be the same as

type someFunc () {
int d[] = {1,2,3,4};
....
}

the array is only valid within the function.the case
"someFunc({1,2,3,4})" doesnt work as far as i know.



Thanks
Ram

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.