• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multi dimensional arrays

 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why we can't access a 2D array like this..?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, but this worked:-It must have something to do with the size of the array.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome Please show us how you printed out those addresses.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic