3

I would like to pass parameters instantly to function, without creating additional variable like this:

void DrawSprite( float a[2], float b[2] ) { /* */ }

DrawSprite( (0.0f, 0.0f), (50.0f, 50.0f) );

Is it possible, and if yes, how to do it?

Neomex
  • 1,650
  • 6
  • 24
  • 38
  • 1
    You dont pass an array to a function, You just pass an pointer to the array in the function.Having said so your question doesnt make any sense. – Alok Save Jan 12 '12 at 16:35
  • @Als no, an array is not a pointer. an array is an array. and that is not to say that passing a primitive array implies copying the array. – wilhelmtell Jan 12 '12 at 16:42
  • 1
    you might want to consider adding a class for your points (easier passing by reference/rvalues with c++0x) or take a look at initializer lists http://stackoverflow.com/questions/5733828/initializing-from-an-initializer-list-but-without(see the first post, not quite pretty but close) – ted Jan 12 '12 at 17:30
  • 1
    @wilhelmtell: I don't see where I said or implied that *array is an pointer*. I fail to see any relevance whatsoever of your comment to what I commented. – Alok Save Jan 13 '12 at 02:44

6 Answers6

4

You could do something like the following (compile with g++ -std=c++11):

#include <array>

typedef std::array<float, 2> Point2f;

void foo(Point2f v) {
  // do something
}

int main() {
  foo( {1.0, 1.0} );
}

The {}-style initialization is called initializer list and it's available from C++11 onwards. The same is true for the std::array.

For reference:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
3

Yes, you can do that.

Try this:

void DrawSprite( float a[2], float b[2] ) { /* */ }

DrawSprite( (float[2]){1.0, 2.0}, (float[2]){3.0, 4.0} );

Including the array size in the second line is good because it lets the compiler enforce the size of the array, but you don't have to.

If the syntax is annoying for you to type, you can use a preprocessor macro:

#define P(X,Y) (float[]){X, Y}
DrawSprite(P(1.0,2.0), P(3.0,4.0));

or perhaps an inline function.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • With something like this it throws lot of syntax errors, also when I changed '{' to '(' it says "cannot convert from 'float' to 'float [2]' " – Neomex Jan 12 '12 at 16:49
  • The first block of code I posted compiles fine for me in MinGW's g++. If you can't figure out the syntax errors you added, ask another question and post your code. – David Grayson Jan 13 '12 at 04:37
  • As already someone wrote here, It is because I use MVC++, which doesn't support it. ( C++0x call? ) – Neomex Jan 14 '12 at 00:44
3

You can call it like this:

float x[2] = {0.0F, 0.0F};
float y[2] = {50.0F, 50.0F};
DrawSprite(x, y);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2
DrawSprite( (float[2]){1.0, 2.0}, (float[2]){3.0, 4.0} );

This works with GCC, but does not work in MSVC. I believe this is not standard C++

Alexander Vassilev
  • 1,399
  • 13
  • 23
1

Some of the answers to this important question seem to be misleading. The issue is: passing a fixed size array to a function. First of all something like

  float x[2] = {0.1F, 0.2F};
  float y[2];
  y=x;

is a error: invalid array assignment (on gcc 4.6.1; but we should be speaking on "standard" here, not particular compiler features). Quite rightly so -- what should the operation accomplish? Both x and y point to a piece of memory on stack, so copying the float* x to y would make little sense, as the values will not be copied to the place reserved initially, y[0],y[1].

The only sensible cast from float x[2] is to float *y. This is precisely what happens when a function is called. Your function DrawSprite(float a[2], float b[2]) is really treated as DrawSprite(float* a, float* b) (you might print out typeid(a).name() to check). Therefore you can pass x to DrawSprite, and this x will simply be cast to float*.

The second issue is initialization. The code (float[2]){1.0, 2.0} really constructs a fixed-size temporary array of float (again can be checked by typeid((float[2]){1.0, 2.0}).name(), at least on gcc), but there is not much you can do with this array, as assignments to float [2] are forbidden. You may cast it to float* (e.g. assign by z=(float[2]){1.0,2.0};, but to float* z;, so it is really z=(float*)(float[2]){1.0,2.0};), or you may pass it to your function, which does the same, i.e. casts to float*.

P Marecki
  • 1,108
  • 15
  • 19
1

better to declare it as an array of float which is declared using malloc . It will enable you to free the array on your will after your work is done . As in malloc memory will be allocated in heap . Passing an array can results in ambiguous stage sometime and lead to crashing of code

Invictus
  • 4,028
  • 10
  • 50
  • 80