Posted: Wed Nov 16, 2005 11:47 pm Post subject: Array of pointers
Is the best way to change a array belonging to the main function inside a different function by using enumerated types & a pointer to it?
Sponsor Sponsor
wtd
Posted: Thu Nov 17, 2005 12:02 am Post subject: (No subject)
Well, you have an array declared inside your main function.
c:
int main() { int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return0;
}
What you really have is a glorified pointer that points to the beginning of the array.
This pointer can be passed to another function. But, since you just have a glorified pointer, no information about the length of the array gets passed with it. So we have a separate parameter for the length of the array.
Then inside the function, we can iterate over the array.
c:
void increment_all(int *input_array, int size_of_array) { int index;
Posted: Thu Nov 17, 2005 12:24 am Post subject: (No subject)
I should have been more specific. How about multi dementional arrays? (2 in the case I am working with)
wtd
Posted: Thu Nov 17, 2005 12:58 am Post subject: (No subject)
Ok. A two-dimensional array is an array of arrays. Each of those arrays is a pointer to its beginning. Thus the two-dimensional array is just a glorified pointer to a glorified pointer.
However, there is a complication. If we have a multidimensional array, we cannot pass declare the function as taking a pointer to a pointer.
Here's some working code to consider:
c:
#include <stdio.h>
void increment_all(int(*input_array)[5], int y_dimension) { int x_index, y_index;
Posted: Thu Nov 17, 2005 1:07 am Post subject: (No subject)
Isn't C silly. You rock my socks wtd.
wtd
Posted: Thu Nov 17, 2005 1:13 am Post subject: (No subject)
Tubs wrote:
Isn't C silly.
Yes.
Tubs wrote:
You rock my socks wtd.
Glad to help.
Thank you for asking a relatively focused question. These are so much easier to approach than, "here's my hundred lines of code... what's wrong?!"
wtd
Posted: Thu Nov 17, 2005 1:19 am Post subject: (No subject)
Yes, C is silly. Fortunately there are other fish in the sea.
code:
# let foo = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|];;
val foo : int array array = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|]
# let increment_all arr =
for y = 0 to Array.length arr - 1 do
for x = 0 to Array.length arr.(y) - 1 do
arr.(y).(x) <- arr.(y).(x) + 1
done
done;;
val increment_all : int array array -> unit = <fun>
# increment_all foo;;
- : unit = ()
# foo;;
- : int array array = [|[|2; 3; 4|]; [|5; 6; 7; 8|]|]
Tubs
Posted: Thu Nov 17, 2005 1:44 am Post subject: (No subject)
*backs away slowly towards the door*
*runs*
Sponsor Sponsor
wtd
Posted: Thu Nov 17, 2005 2:08 am Post subject: (No subject)
Here's just the function:
code:
let increment_all arr =
for y = 0 to Array.length arr - 1 do
for x = 0 to Array.length arr.(y) - 1 do
arr.(y).(x) <- arr.(y).(x) + 1
done
done
That isn't so bad, is it?
Tubs
Posted: Thu Nov 17, 2005 12:31 pm Post subject: (No subject)
Not trying to be a nuisance and post my 100 line program and ask why its not working, but I really don't know how to solve this problem. Since this program uses reading / writing from .txt files it crashes inside my IDE, but says that there are no errors. However, when i try to run the .exe that has a windows error as well so I am left with absolutely no idea on what is failing (I suspect it has something to do with the borders of the array). The .txt file is a 25x25 square of X's, with -'s along the border.