Is it possible to make a user-defined array function? Thank you so much!

 

Excuse me,

I was wondering whether anyone superior here could enlighten me.

Is it possible to make a user-defined array function?

If it could be done, we must be able to transfer the array name as a parameter to the function.

Could someone give me an example?

Thank you so much!

 

You pass the array name by reference . . . for example . . .

double Array[];
double AnotherArray[];

ResizeArray(Array, 30);
ResizeArray(AnotherArray, 200);


void ResizeArray(double& ArrayToBeResized[], int NumberOfElements)
   {      

   ArrayResize(ArrayToBeResized, NumberOfElements);

   }
 
RaptorUK:

You pass the array name by reference . . . for example . . .


Thank you so much, RaptorUK!!!


By the way, could you please show me another example on how to make a multi-dimension array function?


Thank you very much!

 
blackkettle:
By the way, could you please show me another example on how to make a multi-dimension array function?
  1. You can only modify the first index of the array
    #define SIZE 10
    static double   vHist[][SIZE];          ArrayResize(vHist, nHist);
    // array is now [nHist][SIZE]
    
  2. If you want both indices to be dynamic, you have to do it yourself

    int dim2; double arr[];
    void   setDim(int d1, int d2){ dim2=d2; arrayResize(arr, d1*d2); }
    void   setArr(int d1, int d2, double v){ arr[d1*dim2+d2] = v;    }
    double getArr(int d1, int d2){ return( arr[d1*dim2+d2] );        }
    :
    setDim(2,3); // looks like array[2][3]
    setArr(1,2,v) // looks like array[1][2]=v

 
WHRoeder:
  1. You can only modify the first index of the array
  2. If you want both indices to be dynamic, you have to do it yourself



Thank you so much, WHRoeder!

Your code is great!