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

Dani AI

Generated

A few clarifications and a simpler, robust alternative that complements the answers already posted.

gave the correct, type-safe way to swap two VLA buffers by using pointers-to-array (that lets the callee reorder buffers without copying). was also correct that swapping the two arguments at the call site is the simplest if the caller can do the swap. ’s int example is incorrect because a true 2‑D array does not decay to `T. ’smemcpy` idea works but copies every element (costly if the goal is to avoid copying).

If the goal is minimal bookkeeping and maximum speed, allocate each matrix as one contiguous 1‑D buffer and index with i*cols + j. Swapping two contiguous buffers then becomes a single pointer swap (no element copies). Example pattern:

float *A = malloc(rows * cols * sizeof *A);
float *B = malloc(rows * cols * sizeof *B);

/* access element (i,j) as A[i*cols + j] */

float *tmp = A;
A = B;
B = tmp;

Notes and pitfalls:

  • If the caller needs to observe the swap, keep the pointers in the caller and swap there, or pass float ** (or the proper pointer‑to‑pointer type) so the callee can modify the caller’s pointers. Swapping local copies inside a function does not change the caller’s variables.
  • Never return or use pointers to function‑local (stack) arrays after the function returns — that produces dangling pointers. Heap allocation (malloc/free) or keeping the real arrays in the caller avoids this.
  • Be consistent with types (original posts mixed int arrays with float parameters) and use zero‑based loops (for (i = 0; i < rows; ++i)), unless there’s a deliberate reason to skip row/column zero.
  • If the storage is not contiguous (e.g., array of pointers to rows), a pointer swap may still be possible by swapping the row pointers, but element‑wise memcpy may be unavoidable when underlying layouts differ.

Summary: for simple, fast buffer swapping avoid per‑element copies by using contiguous buffers and swapping the pointers (or use the pointer‑to‑array approach shown by if VLAs and strict types are preferred).

Recommended Answers

All 8 Replies

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.