How to pass double[][] into a method call?

后端 未结 3 928
温柔的废话
温柔的废话 2021-01-15 15:08

Hi I am trying to init an object with a double value in the format double filter[3][3]; but i keep getting the following error.

cannot convert \'double[3][3]\' to \'

3条回答
  •  时光取名叫无心
    2021-01-15 15:10

    A two-dimensional array is not the same thing as a pointer-to-a-pointer. You have two choices - change the filter class to contain a 2D array, or change your initialization to use pointer-to-pointers.

    In choice #1, you're could keep a copy of the array in your filter instance, instead of just holding a pointer. You need to change the class interface:

    @interface filter : NSObject
    {
        double matrix[3][3];
    }
    
    -(id)initWithMatrix:(double[3][3])filterMatrix;
    

    Then your implementation of initWithMatrix: can just do a memcpy() or the equivalent to copy the data into your instance.

    Choice #2 is a bit different. Keep your other code the way it is, but change your initialization of filter:

    double row0[3] = {0,0,0};
    double row1[3] = {0,1,0};
    double row2[3] = {0,0,0};
    double **filter[3] = { row0, row1, row2 };
    

    It's probably safer to malloc() all of those arrays, since otherwise you're going to end up with references to stack variables in your filter class, but I think you get the idea.

提交回复
热议问题