User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 422,412 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,046 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 503 | Replies: 8 | Solved
Join Date: Nov 2006
Location: Athens, Greece
Posts: 198
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Swap an array...

  #1  
May 16th, 2008
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. }
  29.  

...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
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC