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 363,830 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 4,119 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:
Views: 361 | Replies: 8 | Solved
Reply
Join Date: Nov 2006
Location: Athens, Greece
Posts: 191
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
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  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 210
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 35
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Whiz in Training

Re: Swap an array...

  #2  
May 16th, 2008
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
KSG
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 191
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: Swap an array...

  #3  
May 16th, 2008
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!
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"
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 146
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Swap an array...

  #4  
May 16th, 2008
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.
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 191
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: Swap an array...

  #5  
May 16th, 2008
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 11:01 am.
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"
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,011
Reputation: vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light 
Rep Power: 9
Solved Threads: 152
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Swap an array...

  #6  
May 16th, 2008
> 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. }
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,702
Reputation: Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all 
Rep Power: 9
Solved Threads: 163
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Swap an array...

  #7  
May 16th, 2008
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.
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 191
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: Swap an array...

  #8  
May 18th, 2008
thanks vijayan121, this solved my problem.

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

-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"
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 191
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: Swap an array...

  #9  
May 18th, 2008
Sorry for the double post, it was an accident.
Last edited by n.aggel : May 18th, 2008 at 8:18 am.
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"
Reply With Quote  
Reply

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

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

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

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