Pointer manipulation bug?

I was applying pointer in a function but sth weird happened...

If I define a 2D array, it works fine, but as soon as I switched 1D, boom...

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Post the boom codeā€¦

We need to see your code and have more details of the problem

Sorry its my first time using the forum

#define N 3
#define Pi 3.1415926

double trans1 [N][N];
double trans2 [N][N];
double transN [N][N];
double pos [N] ;
double pos2 [N];
double pos3 [N];



double alpha = 90;
double beta = 30;


double L1 = 10;
double L2 = 20;


//position vector creator
void PosCreate (double length, double angle, double *p);





void setup() {
  // put your setup code here, to run once:

  PosCreate(L1, alpha, *trans1);
  PosCreate(L2, beta, trans2);
  PosCreate(L1, alpha, *pos);
  PosCreate(L1, alpha, pos2);

  

      
}

void loop() {
  // put your main code here, to run repeatedly:

}



void PosCreate (double length, double angle, double *p){
  
  //lengths
  *p = length * cos(angle/2);
  *(p + 1) = length * sin(angle/2);

  //constant
  *(p + 2) = 1;
}

D:\Arduino codes\sketch_sep5d\sketch_sep5d.ino: In function 'void setup()':
D:\Arduino codes\sketch_sep5d\sketch_sep5d.ino:32:29: error: cannot convert 'double (*)[3]' to 'double*' for argument '3' to 'void PosCreate(double, double, double*)'
   PosCreate(L2, beta, *trans2);
                             ^
G:\Arduino codes\sketch_sep5d\sketch_sep5d.ino:33:28: error: cannot convert 'double' to 'double*' for argument '3' to 'void PosCreate(double, double, double*)'
   PosCreate(L1, alpha, *pos);
                            ^

exit status 1

Compilation error: cannot convert 'double (*)[3]' to 'double*' for argument '3' to 'void PosCreate(double, double, double*)'

Try:

void PosCreate(double length, double angle, double p[][N]) {

The function body probably does not make any sense for a 2-dimensional array though.

[edit]

You could overload the PosCreate function if you want to have functionality for both types of arrays.

size_t const N = 3;
double x[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
double y[N] = {1, 2, 3};

void PosCreate(double length, double angle, double p[][N]) {
  for (size_t i = 0; i < N; i++) {
    for (size_t j = 0; j < N; j++) {
      Serial.println(p[i][j]);
    }
  }
}

void PosCreate(double length, double angle, double p[]) {
  for (size_t i = 0; i < N; i++) {
    Serial.println(p[i]);
  }
}

void setup() {
  Serial.begin(9600);

  PosCreate(1, 2, x);
  PosCreate(1, 2, y);
}

void loop() {}

void PosCreate (double length, double angle, double **p){ actually works! though i dont know why lol Thank you!

Strange, it should not... You might want to have a look at the edited post.

shouldn't this be, for example
PosCreate(L1, alpha, & trans1 [2][0]);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.