2

I've some problems to use pointers in C++. I implemented three methods with different sized arrays but the same calculations. So I decided to extract the calculations and place them in a new method which expects an array. But that doesn't work and I don't know how to modify my program.

void method1() {
  float a[3][3];
  calculate(a, 3);
}

void method2() {
  float a[4][4];
  calculate(a, 4);
}

void method3() {
  float a[5][5];
  calculate(a, 5);
}

void calculate(float *param[], int n) {
  // Code
}

I know that I can use global variables or vectors etc. but I need the logic in this structure.

Here's the compiler error:

Test.cpp: In function 'void method1()':
Test.cpp:7:16: error: cannot convert 'float (*)[3]' to 'float**' for argument '1' to 'void calculate(float**, int)'
Test.cpp: In function 'void method2()':
Test.cpp:12:16: error: cannot convert 'float (*)[4]' to 'float**' for argument '1' to 'void calculate(float**, int)'
Test.cpp: In function 'void method3()':
Test.cpp:17:16: error: cannot convert 'float (*)[5]' to 'float**' for argument '1' to 'void calculate(float**, int)'

Thanks in advance!

1

4 Answers 4

5
template <int n>
void calculate(float (&param)[n][n]) {
  std::cout << param[x][y];
}

void method1() {
  float a[3][3];
  calculate(a);
}

or, if you want to support dynamic sizes (unlikely), you'll have to do a wierd trick and lie to the compiler pretending that it's a 1d array, and that gets tricky fast.

int index(int x, int y, int n) {return y*n+x;}

void calculate(float* param, int n) {
    std::cout << param[index(x, y, n)];
}

void method1() {
  float a[3][3];
  calculate(a[0], 3);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great! I took the template version and it works fine. Thanks a lot!
1

If you wish to pass the array without using a template, you may do something like:

static const int size = 3;

void calculate(float (&param)[size][size]) {
}

int main() {
  float  a[size][size];

  calculate(a);
  return 0;
}

size can also be a #define, but i prefer static const.

Comments

1

You can use a pointer to pointer...

void calculate(float *param[], int n)
{
    // Code
}

void method1() {

    float **a = new float*[3];

    a[0] = new float[3];
    a[1] = new float[3];
    a[2] = new float[3];

    a[0][0] = 1.0f;
    a[0][1] = 2.0f;
    a[0][2] = 3.0f;

    a[1][0] = 4.0f;
    a[1][1] = 5.0f;
    a[1][2] = 6.0f;

    a[2][0] = 7.0f;
    a[2][1] = 8.0f;
    a[2][2] = 9.0f;

    calculate(a, 3);
}


void method2() {
    float **a = new float*[4];

    a[0] = new float[4];
    a[1] = new float[4];
    a[2] = new float[4];
    a[3] = new float[4];

    calculate(a, 4);
}

void method3() {
    float **a = new float*[5];

    a[0] = new float[5];
    a[1] = new float[5];
    a[2] = new float[5];
    a[3] = new float[5];
    a[4] = new float[5];

    calculate(a, 5);
}

Otherwise you have to specify the column size of the array in the function signature...

void calculate(float param[][3], int n) {
  // Code
}

void method1() {
  float a[3][3];
  calculate(a, 3);
}
/*
void method2() {
  float a[4][4];
  calculate(a, 4);
}

void method3() {
  float a[5][5];
  calculate(a, 5);
}*/

Comments

0

If you want to handle only square arrays, then you may use template:

template <int N>
calculate(float param[N][N]) {
// code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.