Function Parameters to Array

 

Hey guys,

Is it posible to place your function parameters to an array like this?

int My_Function(int a, int b, int c)
        {
        int myArray[3] = {a, b, c}
        }

Im not really sure if it's possible coz I'm getting an error.

 
Arcee Palabrica:

Hey guys,

Is it posible to place your function parameters to an array like this?

Im not really sure if it's possible coz I'm getting an error.

what error ?
 
Arcee Palabrica:

Hey guys,

Is it posible to place your function parameters to an array like this?

Im not really sure if it's possible coz I'm getting an error.

int myArray[3] = {a, b, c}

You cannot use a,b,c you must use constants

or

int myArray[3];
myArray[0]=a;
myArray[1]=b;
myArray[2]=c;
 
Soewono Effendi:
what error ?

this is what i got

'a' - constant expression required

'b' - constant expression required

'c' - constant expression required

 
Keith Watford:

You cannot use a,b,c you must use constants

or

Wow thanks. Is it be possible to loop them incase. so it's easier in case we increase the number of indices?

 
Arcee Palabrica:

Wow thanks. Is it be possible to loop them incase. so it's easier in case we increase the number of indices?

I can't think of a way that would be useful. You could pass an array with the values but that would rather defeat the object of having a function to fill an array when you have already created an array.

Depending on your code, instead of calculating values for a,b and c, can you place the value straight into an array?

instead of

a=x*y;

use

myArray[0]=x*y;
Reason: