473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Are Anonymous arrays possible


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
3 7385
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: moller | last post by:
I'm reposting this under a more appropriate subject since excel really has nothing to do with my problem. I'm new to perl but not to programming in general. I have been trying to put some...
1
by: jose luis fernandez diaz | last post by:
Hi, In the chapter 4 'Collections and Records' of the 'PL/SQL User's Guide and Reference Release 8.1.6' book there is the next paragrap: "For example, PL/SQL supports implicit (automatic)...
4
by: Vannela | last post by:
How can i achieve the dynamic arrays concept in C#? I have used the ArrayLists of C# but in few cases it is failing like when i pass these dynamic arrays a arguments to functions b'coz in those...
6
by: Daniel O'Connell | last post by:
Two questions here: 1. Is there any particular reason why when using stackalloc, the code byte *buffer = stackalloc byte; works, but code like byte *buffer; buffer = stackalloc byte; is...
10
by: ellie | last post by:
How do you add elements to an array defined within another class? For instance, class CEntity { public: int num_inPort; int num_outPort;
6
by: Anders Thomsen | last post by:
Hi, Is it possible on VB.NET to create anonymous arrays, e.g. passing arrays directly as a method-parameter instead of reference it from a variable. In C#, I can do this: string s = "Blah...
2
by: prakashspatil | last post by:
can any body clear the concept of anonymous arrays .
12
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to initialize my class level dictionary (in .Net 2.0). I wanted to make it inline and employ anonymous methods as I do not use this code for anything else. Something similar to the...
9
by: andreyvul | last post by:
I'm trying to do the following: typedef struct { char *bar; char **baz; } foo; const foo fop = { { "foo", { "bar", "baz", "bax" } }, { "goo", { "car", "cdr", "cfr" } }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.