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?
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?
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:
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.
You can call it like this:
float x[2] = {0.0F, 0.0F};
float y[2] = {50.0F, 50.0F};
DrawSprite(x, y);
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++
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*
.
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