0

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

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by spetro3387
0

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; }

Edited by m4ster_r0shi: n/a

0

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

-Sam

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.