d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Passing Multidimensional Arrays To Functions > Wut
Add Reply New Topic New Poll
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
May 25 2012 09:46am
What's the deal with not being able to pass a two-dimensional array to a function without specifying the second dimension's size? This makes creating a function that will handle differently sized multidimensional arrays a pain to create a work around.
I can work around this by making a one dimensional array of pointers to other arrays (I believe), but I'm asking more along the lines of why it won't let you just put "myArray[][]" as a function argument without the sizes specified, but "myArray[]" and "myArray[][8]" would work.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
May 25 2012 10:47am
couldnt you just pass a pointer to your function pointing to myarray[][]
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
May 25 2012 11:48am
Quote (carteblanche @ May 25 2012 12:41pm)


Interesting, I had been taught and had read that multidimensional arrays are actually just normal single dimensional arrays of size length*width that are abstracted for the programmer's ease of use and readability. That page though, says that multidimensional arrays are actually arrays of other arrays.
Though, I still don't fully understand - it said that "the function must know that a is an array of arrays of 7 ints". Why does it need to know this? Shouldn't it just need to know that the datatype is an integer?
If we have an array myArray[3][4], when I pass it to the function the value that's actually passed is a pointer to the first element, correct? So then, if in that function I asked for myArray[1][2], shouldn't it know to go to the element after the pointer it had received, use that pointer to find the array, and then go to the third element of that array? Since it knows that the data being stored are integers, shouldn't it know how far it needs to jump around in memory to access the correct elements? I still don't see why the function cares what the second dimension's size is.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 25 2012 05:11pm
for the C world, keep in mind that array[offset] is the same as (array+offset)*. im not big on C so i could be wrong, but it looks like an array of arrays is the same thing as array of length dim1*dim2, unlike java where array is an actual object

So lets say this is what your memory looks like
array[index1][index2] for size array[2][5]

relative address: index1, index2

0: 0, 0
1: 0, 1
2: 0, 2
3: 0, 3
4: 0, 4
5: 1, 0
6: 1, 1
7: 1, 2
8: 1, 3
9: 1, 4

array[0] points to 0
array[1] points to 5

using relative address where your datatype fits in a single address for simplicity.

so suppose you don't know the length of the second dimension, so you dont know to check 5 spots ahead of the previous index, and you want to access array[1][0] .Tell me, how do you find the address? Remember, you don't know about the number 5.

This post was edited by carteblanche on May 25 2012 05:12pm
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
May 27 2012 01:44pm
Well, I guess that's where I didn't fully understand. Given your array[2][5], I imagined two arrays being created, A and B, each of length 5. Then, a final array, let's call it C, of length 2 being created, with index 0 holding the address of the first element of array A, and index 1 holding the address of the first element of array B. In that scenario, when you ask for array[1][0] as you did, it would be as simple as asking for the location stored in C[1], and then shifting to the index 0 of the array stored there.

I guess that answers my question then, if multidimensional arrays are stored the way you wrote it out. I don't know if I'd call that an array of arrays though...

Thanks :)

This post was edited by SoulScythe on May 27 2012 01:45pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 27 2012 06:07pm
Quote (SoulScythe @ May 27 2012 03:44pm)
Well, I guess that's where I didn't fully understand. Given your array[2][5], I imagined two arrays being created, A and B, each of length 5. Then, a final array, let's call it C, of length 2 being created, with index 0 holding the address of the first element of array A, and index 1 holding the address of the first element of array B. In that scenario, when you ask for array[1][0] as you did, it would be as simple as asking for the location stored in C[1], and then shifting to the index 0 of the array stored there.

I guess that answers my question then, if multidimensional arrays are stored the way you wrote it out.  I don't know if I'd call that an array of arrays though...

Thanks :)


do not think of an array as an object in the C world. that concept exists in C# and java just fine where they know their own lengths (and hence can be passed like you want), but in C they are NOT objects. the [] syntax is just a SHORTCUT. you simply malloc a few contiguous slots in memory, then access them sequentially. for an array of length 5, C will let you access element array[15] without complaints because it's just syntax sugar for (array + 15)*
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
May 28 2012 12:10am
Quote (carteblanche @ 27 May 2012 18:07)
do not think of an array as an object in the C world. that concept exists in C# and java just fine where they know their own lengths (and hence can be passed like you want), but in C they are NOT objects. the [] syntax is just a SHORTCUT. you simply malloc a few contiguous slots in memory, then access them sequentially. for an array of length 5, C will let you access element array[15] without complaints because it's just syntax sugar for (array + 15)*


Which offset is your brain located at..? I want to point as your heart and work my way up! ;)

Code
(carteblanche + 666)*


Haha..
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll