hi guys, it's been a while since i used c for anything so here goes a question tha may seem silly.


I have 2 variable length arrays....

int main()
{
	int n = 5;
	int m = 6;

	int before[n][m];
	int after[n][m];
	array_function( 5, 6, before, after );
}

void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
{
	//for some iterations	
	for(;;)
	{

		//for all the elements int the array
		for (i=1; i<rows; i++)
			for(j=1; j<cols; j++)
				after[i][j] = 4 * before[i][j]

		//afterwards prepare for the next iteration [1]
		for (i=1; i<rows; i++)
			for(j=1; j<cols; j++)
				before[i][j]=after[i][j];
	}
}

...What i want to do is find a way to swap the pointers to the arrays. So that i can get rid of the last loop.
I figured that since i only need to calculate "after" based on "before" and then make "after"->"before" and recalculate "after".
It would save a lot of time if i just swapped the 2 pointers.

i tried the following but with no luck:

//define function like this:
void array_function(int rows, int cols, float &after[rows][cols] , float &before[rows][cols])

//call it like this:
array_function( 5, 6, &before, &after );

thanks in advance
-nicolas

Hi
U can try Memory functions like memcopy () .... something like that for quick swapping. I dont much about memory functions. but it may helpful to fast swapping

the way i think it, i try to avoid copying anything than 2 pointers... maybe the thread title is a bit misleading

maybe a moderator could change to something like "swap the pointers to 2 arrays" or anything better!

may this will work:

function swap_array(int **after, int **before) {
int **temp = after;
after = before;
before = temp;

}

I have not checked it. Hope it will work.

> i try to avoid copying anything than 2 pointers...
swapping pointers is easy; swap pointers to arrays just like you swap any other pointers.

#include <stdio.h>

void swap( int rows, int cols, float (**after)[rows][cols] , 
           float (**before)[rows][cols] )
{
  float (*temp)[rows][cols] = *after ;
  *after = *before ;
  *before = temp ;
}

int main()
{
  int rows = 3, cols = 4 ;
  float one[rows][cols] ; one[0][0] = 1.1 ;
  float two[rows][cols] ; two[0][0] = 2.2 ;
  float (*p1)[rows][cols] = &one ; 
  float (*p2)[rows][cols] = &two ; 
  printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ; 
  swap( rows, cols, &p1, &p2 ) ;
  printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ; 
}

this means that you will have to write the array_function in terms of pointers to arrays:

void array_function( int rows, int cols, float (*after)[rows][cols],
                     float (*before)[rows][cols] )
{
	//for some iterations	
	for(;;)
	{
		int i, j ;
		//for all the elements int the array
		for (i=1; i<rows; i++)
			for(j=1; j<cols; j++)
				(*after)[i][j] = 4 * (*before)[i][j] ;

		//afterwards prepare for the next iteration
		swap( rows, cols, &after, &before ) ;
	}
}
commented: always helpful! +2

Good grief!

n.aggel, you had the right idea to begin with, only your syntax needs help. Don't change the definition of the function, but just pass the appropriate pointer.

void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  {
  ...
  }

int main()
  {
  int n = 5;
  int m = 6;

  int before[n][m];
  int after[n][m];

  array_function( 5, 6, before, after );  // this works
  array_function( 5, 6, after, before );  // this works

  ...
  }

Hope this helps.

thanks vijayan121, this solved my problem.

Duoas, i wanted to swap the arrays inside the function...

-nicolas

Sorry for the double post, it was an accident.

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.