943,845 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5732
  • C RSS
May 16th, 2008
0

Swap an array...

Expand Post »
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....

  1.  
  2. int main()
  3. {
  4. int n = 5;
  5. int m = 6;
  6.  
  7. int before[n][m];
  8. int after[n][m];
  9. array_function( 5, 6, before, after );
  10. }
  11.  
  12. void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  13. {
  14. //for some iterations
  15. for(;;)
  16. {
  17.  
  18. //for all the elements int the array
  19. for (i=1; i<rows; i++)
  20. for(j=1; j<cols; j++)
  21. after[i][j] = 4 * before[i][j]
  22.  
  23. //afterwards prepare for the next iteration [1]
  24. for (i=1; i<rows; i++)
  25. for(j=1; j<cols; j++)
  26. before[i][j]=after[i][j];
  27. }
  28. }

...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:

  1. //define function like this:
  2. void array_function(int rows, int cols, float &after[rows][cols] , float &before[rows][cols])
  3.  
  4. //call it like this:
  5. array_function( 5, 6, &before, &after );

thanks in advance
-nicolas
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
May 16th, 2008
0

Re: Swap an array...

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
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
May 16th, 2008
0

Re: Swap an array...

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!
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
May 16th, 2008
0

Re: Swap an array...

may this will work:
  1. function swap_array(int **after, int **before) {
  2. int **temp = after;
  3. after = before;
  4. before = temp;
  5.  
  6. }


I have not checked it. Hope it will work.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
May 16th, 2008
0

Re: Swap an array...

nop man this won't work.... because i am asking about variable lenght arrays....

check this out::

http://www.ddj.com/cpp/184401444
Last edited by n.aggel; May 16th, 2008 at 12:01 pm.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
May 16th, 2008
1

Re: Swap an array...

> i try to avoid copying anything than 2 pointers...
swapping pointers is easy; swap pointers to arrays just like you swap any other pointers.
  1. #include <stdio.h>
  2.  
  3. void swap( int rows, int cols, float (**after)[rows][cols] ,
  4. float (**before)[rows][cols] )
  5. {
  6. float (*temp)[rows][cols] = *after ;
  7. *after = *before ;
  8. *before = temp ;
  9. }
  10.  
  11. int main()
  12. {
  13. int rows = 3, cols = 4 ;
  14. float one[rows][cols] ; one[0][0] = 1.1 ;
  15. float two[rows][cols] ; two[0][0] = 2.2 ;
  16. float (*p1)[rows][cols] = &one ;
  17. float (*p2)[rows][cols] = &two ;
  18. printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ;
  19. swap( rows, cols, &p1, &p2 ) ;
  20. printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ;
  21. }

this means that you will have to write the array_function in terms of pointers to arrays:
  1. void array_function( int rows, int cols, float (*after)[rows][cols],
  2. float (*before)[rows][cols] )
  3. {
  4. //for some iterations
  5. for(;;)
  6. {
  7. int i, j ;
  8. //for all the elements int the array
  9. for (i=1; i<rows; i++)
  10. for(j=1; j<cols; j++)
  11. (*after)[i][j] = 4 * (*before)[i][j] ;
  12.  
  13. //afterwards prepare for the next iteration
  14. swap( rows, cols, &after, &before ) ;
  15. }
  16. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 16th, 2008
0

Re: Swap an array...

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.

  1. void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  2. {
  3. ...
  4. }
  5.  
  6. int main()
  7. {
  8. int n = 5;
  9. int m = 6;
  10.  
  11. int before[n][m];
  12. int after[n][m];
  13.  
  14. array_function( 5, 6, before, after ); // this works
  15. array_function( 5, 6, after, before ); // this works
  16.  
  17. ...
  18. }

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 18th, 2008
0

Re: Swap an array...

thanks vijayan121, this solved my problem.

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

-nicolas
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
May 18th, 2008
0

Re: Swap an array...

Sorry for the double post, it was an accident.
Last edited by n.aggel; May 18th, 2008 at 9:18 am.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: program just hangs
Next Thread in C Forum Timeline: How do i use EOF in my program ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC