•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,101 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 2,176 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: 512 | Replies: 8 | Solved
![]() |
| |
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 198
Reputation:
Rep Power: 2
Solved Threads: 9
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"
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 459
Reputation:
Rep Power: 1
Solved Threads: 79
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 198
Reputation:
Rep Power: 2
Solved Threads: 9
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"
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation:
Rep Power: 3
Solved Threads: 17
may this will work:
I have not checked it. Hope it 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.
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 198
Reputation:
Rep Power: 2
Solved Threads: 9
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 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"
by Robert Frost the "The Road Not Taken"
•
•
Join Date: Dec 2006
Location: india
Posts: 1,074
Reputation:
Rep Power: 9
Solved Threads: 161
> 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.
c99 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:
c99 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 ) ; } }
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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.
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 198
Reputation:
Rep Power: 2
Solved Threads: 9
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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 ??



Hybrid Mode