I am trying to change assign the pointer "c" to the pointer "a" below using the function "change()", but I am doing something incorrectly, not exactly sure. The direct assignment within the main() function works (ie a = c), but does not work using the change function.

void change(float (*d)[3], float (*e)[3]){
  d = e;
}


main(){
  float (*a)[3], (*b)[3], c[3][3];
  int i, j;
  for(i = 0; i < 3; i++){
    for(j = 0; j < 3; j++){
      c[i][j] = 5;
    }
  }

  //a = c
  change(a, c);

  for(i = 0; i < 3; i++){
    for(j = 0; j < 3; j++){
      cout << a[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}

Thanks.

-Sam

Recommended Answers

All 2 Replies

If you want to modify something inside a function, you should pass it by reference ;)
This will work -> void change(float (*[B]&[/B]d)[3], float (*e)[3]) { d = e; }

Thanks you so much. I kept trying things with ampersands. That was like the one thing I didn't try. Thanks again.

-Sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.