Alex Kizub wrote:
>
> In C++ array is always (physically) one dimension array which could be
> translated to many dimensions. For example:
> [ 1, 2, 3, 4, 5, 6, 7, 8 ] could be reffered as int[8] or int[2][4] or
> even int[2][2][2].
Surprising; I'd imagined that C++ was similar to C insofar
as "shared concepts" are concerned, but from what you say it
must be quite different.
> All elements are similar type.
>
> In Java only lower level is this type when all other are arrays!
> In your example stud[1][1] is the Student but stud[1] is array of
> Students.
> Which brings interesting idea/implementation of varydimensioanl
> arrays. Watch this and tell me how you can do this in C++?
>
> stud: *---> [ * * * ]
> | | |
> | | +---> [ * ]
> | | |
> | | v
> | | (S)
> | |
> | +---> [ * * * ]
> | | | |
> | v v v
> | (S)(S)(S)
> |
> +---> [ * * * * * * * * * ]
> | | | null | | | |
> v v v v v v v
> (S)(S)(S) (S)(S)(S)(S)
> It's possible because stud[][] is array of array and each array could
> be different.
I cannot answer for C++ because I don't know the language,
and from what you said earlier it must be very different from C.
But in C, this outline is easy to achieve, and in fact the
resulting structure has some similarities to Java's arrays:
/* C source (no error checking) */
Student **array;
array = malloc(3 * sizeof *array);
array[0] = malloc(9 * sizeof *array[0]);
array[0][0] = newStudent();
array[0][1] = newStudent();
array[0][2] = newStudent();
array[0][3] = NULL;
array[0][4] = NULL;
array[0][5] = newStudent();
array[0][6] = newStudent();
array[0][7] = newStudent();
array[0][8] = newStudent();
array[1] = malloc(3 * sizeof *array[1]);
array[1][0] = newStudent();
array[1][1] = newStudent();
array[1][2] = newStudent();
array[2] = malloc(1 * sizeof *array[2]);
array[2][0] = newStudent();
The above is approximately equivalent to
// Java source (could be shortened)
Student[][] array = new Student[3][];
array[0] = new Student[9];
array[0][0] = new Student();
array[0][1] = new Student();
array[0][2] = new Student();
// array[0][3] and array[0][4] already == null
array[0][5] = new Student();
array[0][6] = new Student();
array[0][7] = new Student();
array[0][8] = new Student();
array[1] = new Student[3];
array[1][0] = new Student();
array[1][1] = new Student();
array[1][2] = new Student();
array[2] = new Student[1];
array[2][0] = new Student();
.... where "approximately" covers a multitude of sins.
--
Eric Sosman
lid