| | |
Swap an array...
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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....
...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:
thanks in advance
-nicolas
I have 2 variable length arrays....
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
//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
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"
by Robert Frost the "The Road Not Taken"
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!
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"
by Robert Frost the "The Road Not Taken"
may this will work:
I have not checked it. Hope it will work.
C Syntax (Toggle Plain Text)
function swap_array(int **after, int **before) { int **temp = after; after = before; before = temp; }
I have not checked it. Hope it will work.
nop man this won't work.... because i am asking about variable lenght arrays....
check this out::
http://www.ddj.com/cpp/184401444
check this out::
http://www.ddj.com/cpp/184401444
Last edited by n.aggel; May 16th, 2008 at 12:01 pm.
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"
by Robert Frost the "The Road Not Taken"
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> i try to avoid copying anything than 2 pointers...
swapping pointers is easy; swap pointers to arrays just like you swap any other pointers.
this means that you will have to write the array_function in terms of pointers to arrays:
swapping pointers is easy; swap pointers to arrays just like you swap any other pointers.
C Syntax (Toggle Plain Text)
#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:
C Syntax (Toggle Plain Text)
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 ) ; } }
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.
Hope this helps.
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.
C Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Help needed filling array with unique random numbers (C++)
- Index array & sort algorithm??? (Java)
- Search program help in modifications (Java)
- Arrays in c/c++ language (C++)
- Need Help Revising Array program: Visual C++ (C++)
- bubble sorting in an array (C)
- Help on assignment (C)
- Need help calculating Median when array is even (C++)
- How can i swap two buttons by clicking on them (Java)
Other Threads in the C Forum
- Previous Thread: program just hangs
- Next Thread: How do i use EOF in my program ??
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






