0

I originally had the code:

double **u_new,**u;

for(iy=0;iy<Ny;iy++) {
  for(ix=0;ix<Nx;ix++) {
    u[ix][iy] = u_new[ix][iy];
  }
}

Here both u and u_new are two dimensional arrays.

These arrays are quite large therefore I believe it is inefficient to do this and I should be swapping their memory locations instead.

I have created a function called swapmem defined as follows:

void swap_mem(double **array1, double **array2) 
{
  double *tmp;
  tmp = *array1;
  *array1 = *array2;
  *array2 = tmp;
}

In my code I call

   swap_mem(&u,&u_new); 

However this doesn't seem to work. I get the warning: passing argument 1 of swap_mem from incompatible pointer type

How can I fix this?

Will C
  • 117
  • 2
  • 10

1 Answers1

1

Change to this and it should work:

void swap_mem(double ***array1, double ***array2)
{
    double **tmp;
    tmp = *array1;
    *array1 = *array2;
    *array2 = tmp;
}

Read warnings and errors carefully. They actually contain good clues for what to do. When I compiled your code I got this:

bb.c:21:14: warning: passing argument 1 of ‘swap_mem’ from incompatible pointer type [-Wincompatible-pointer-types]
     swap_mem(&u, &u_new);
              ^
bb.c:10:6: note: expected ‘double **’ but argument is of type ‘double ***’
 void swap_mem(double **array1, double **array2)

Note the line that says: expected ‘double **’ but argument is of type ‘double ***

klutt
  • 30,332
  • 17
  • 55
  • 95
  • [See here](https://stackoverflow.com/questions/3982348/implement-generic-swap-macro-in-c) for generic swap – M.M Nov 05 '17 at 20:55