aspose file tools*
The moose likes C / C++ and the fly likes multi dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "multi dimensional arrays" Watch "multi dimensional arrays" New topic
Author

multi dimensional arrays

Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1



Why we can't access a 2D array like this..?


Ramesh-X
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Don't know, but this worked:-It must have something to do with the size of the array.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

We can define the function as follows too..

No need of 3 there.
In 3D functions we have to write,,

I want to know is, what is the need of writing the bounds of the array to the compiler..?

'
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
And you find that C “arrays” don't have bounds. C has no way of distinguishing int array[3][2] from int array[2][3].
I copied and pasted my code from yesterday and enhanced it, getting this warning:-
[campbell@localhost ~]$ gcc -o multi multi.c
multi.c: In function ‘main’:
multi.c:34:2: warning: passing argument 1 of ‘print’ from incompatible pointer type [enabled by default]
multi.c:16:6: note: expected ‘int (*)[3]’ but argument is of type ‘int (*)[2]’
Then I ran it with the simple command ./multi. Try it. See what happens. See what happens if you run it several times.

C is not a type‑safe language. Although gcc gave that warning, it is a permissible program in C. Since C arrays are simply glorified pointers, they have no bounds, and you have to tell the compiler how many elements there are in each “row”. In Java, which has arrays of arrays, that problem would not arise. Nor would the possibility of going beyond the intended bounds of the array.

Or, to put is differently, your array is not stored as {1, 2}, {3, 4}, {5, 6}. It is stored as 1 2 3 4 5 6. Unless you specifically tell the function to divide it into subarrays length 2, it cannot cope.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Earlier, I wrote: . . . I copied and pasted my code from yesterday and enhanced it . . .
Should I have posted it? Maybe I should have:-
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

Thank you..
Your answer was helpful.
In the RAM, the int variable are created in an array, but no sign is stored to check the dimension of the array.. I checked it by writing a program to print the addresses of the elements in the array..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
You're welcome Please show us how you printed out those addresses.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1



When run this code, the addresses of the array are printed. But if there is no difference between 2D array and 1D array when storing in the RAM the following code must give the same result..


I think what this do is only creating the space to an array with 3x2 elements. That means if we create an array like, int arr[2][3][4]; space will be allocated to an array with 2x3x4 elements..



Here we have written to create an array with 3x2 elements. But only 5 elements are given. Therefore 6th element is printed as 0...

As the dimension is not stored, we have to give the size of the sub arrays in parameters. I think it is the answer for my question..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I think you are right, yes. There is some nice code there (if there can be anything nice about pointer arithmetic). I put all your code together and got differint addresses:-
[campbell@localhost CPrograms]$ ./pointers

2D array
0x7fffec4be9e0, 0x7fffec4be9e4
0x7fffec4be9e8, 0x7fffec4be9ec
0x7fffec4be9f0, 0x7fffec4be9f4

1D array as 2D
0x7fffec4be9c0 --> 1
0x7fffec4be9c4 --> 2
0x7fffec4be9c8 --> 3
0x7fffec4be9cc --> 4
0x7fffec4be9d0 --> 5
0x7fffec4be9d4 --> 6

6-element 2D array with 5 values
0x7fffec4be9a0 --> 1
0x7fffec4be9a4 --> 2
0x7fffec4be9a8 --> 3
0x7fffec4be9ac --> 4
0x7fffec4be9b0 --> 5
0x7fffec4be9b4 --> 0
Note the memory addresses are slightly different for each array, apparently going backwards by 0x20 at a time, and the addresses differ by 4 bytes and my machine uses twelve (!!) digits for its addresses.
Samantha Rose
Greenhorn

Joined: Jul 22, 2013
Posts: 7
You can't pass a fully-dynamic multi-dimensional array as a function parameter. C++ gets really cranky about memory allocation when it comes to parameters. It wants to know what to expect and how much memory it can assume it needs. It will let you get away with passing one dynamic size (like with int[5][] or int[5][4][3][2][]) however.

Dynamic arrays are like they say, dynamically allocated within memory at run time. Your program makes guesses in how much space to allocate for a dynamic array. These are called memory chunks. Usually it's an overestimation (exactly what I cannot particularly remember), but if it finds it needs more space, it has to allocate a new chunk, which means more space needed for your program to run. This can get quickly out of hand for larger multi-dimensional arrays.

If you can get away with it, I'd recommend using static arrays.

If not and you absolutely have to use a dynamic array as a parameter, I'd suggest writing a small wrapper class or struct to hold the array and pass that instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multi dimensional arrays