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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
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? Bothxandypoint to a piece of memory on stack, so copying thefloat*xtoywould 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 tofloat *y. This is precisely what happens when a function is called. Your functionDrawSprite(float a[2], float b[2])is really treated asDrawSprite(float* a, float* b)(you might print outtypeid(a).name()to check). Therefore you can passxtoDrawSprite, and thisxwill simply be cast tofloat*.The second issue is initialization. The code
(float[2]){1.0, 2.0}really constructs a fixed-size temporary array offloat(again can be checked bytypeid((float[2]){1.0, 2.0}).name(), at least ongcc), but there is not much you can do with this array, as assignments tofloat [2]are forbidden. You may cast it tofloat*(e.g. assign byz=(float[2]){1.0,2.0};, but tofloat* z;, so it is reallyz=(float*)(float[2]){1.0,2.0};), or you may pass it to your function, which does the same, i.e. casts tofloat*.