Alex Rivera | Logout

Pass by reference through multiple functions

Asked 2009-04-04T19:09:56.823
9

Hey all. I'm working on a project for school where I need to pass a few parameters by reference through multiple functions. I understand how I can pass by reference from where the variables are declared to another function, like this:

main() {
  int x = 0;
  int y = 0;
  int z = 0;

  foo_function(&x, &y, &z);
}

int foo_function(int* x, int* y, int* z) {
  *x = *y * *z;
  return 0;
}

However, how would I pass x, y, and z from foo function to another function? Something like this gives me all kinds of compiler warnings.

int foo_function(int* x,  int* y, int* z) {
  *x = *y * *z;
  bar(&x, &y, &z);
  return 0;
}

int bar(int* x, int* y, int* z) {
  //some stuff
}
Edit
Report

2 Answers

2

In foo_function x y and z are already pointers (int*), so you can do bar(x, y, z).

answered 2009-04-04T19:13:17.907
2
int foo_function(int* x,  int* y, int* z) {
   *x = *y * *z;
   /* x, y and z are pointers to int
      &x, &y and &z are pointers to pointer to int
      bar expects pointers to int, so call bar as:
   */
   bar(x, y, z);
   return 0;
}
answered 2009-04-04T19:14:27.937

Your Answer