References and Multidimensional arrays

Is is possible to make only a portion of a multidimensional array constant references and use the rest as normal?

int array [3]
int multiArray [3][2]

I want to use multiArray [x][0] as normal, but make multiArray [x][1] constant references to array [3].
Last edited on
You probably could set up something like that with Pointers.
That still doesn't explain how to do it. I have no clue how I'd differentiate between the int half and the int reference/pointer (whatever one is usable) half in the syntax.
As far as I'm concerned all arrays with known bounds have the different types, it helps the compiler to resolve arrays at the compile time.

Based on this you can 'do the cast trick' to create a reference to slice an array.

1
2
3
4
5
6
int multArr[3][3] = {
        1,2,3,
        4,5,6,
        7,8,9 };

int (&arr)[3] = static_cast<int(&)[3]>(multArr[2]);


However, such a trick has to be abandoned in favour of the valarray<> container slices.
According to Stroustrup C++ manual I've read one should never use a C-style-array against a valarray container. Valarray can be used in a slice() method to generate a subarray and it is more safe.

Sorry, my mistake. valarray<> -- is a common substitution for C-style-arrays in C++.
Last edited on
Your code snippet fills an array with parts of a multidimensional array. What I want to do is make part of a multidimensional array references to another array.
Necrogigas>Your code snippet fills an array with parts of

If by word fills you want to say copies, that is not true.
Exposed by the expression -- be passed as a parameter to the cast template -- multArr[2] represents the pointer to the one-dimensional array starting at the element multArr[2][0] and containing three elements. The type of multArr[2] is (*)[3], so it is not possible to initialize reference with pointer, thus I place a static_cast to pretend that address returned by mulArr[2] is address to the correct one-dimensional array of three elements. Indeed, the object is correct, and after cast I can initialize the reference by the multArr[2] in a predictable manner.

What has happened at the end?
1
2
std::cout << multArr[2] << std::endl;
std::cout << &arr << std::endl;


the addressed you will see on the console output will be the same!

So, arr is not copy of any part of multArr, it only referred to subarray of multArr.
It does not contain elements. Still, you may think about arr as an ordinal automatic array. That what references usually do.

For instance, if you code:
 
arr[1] = 10;

there no any changes in arr, cause` it does not exist! The element multArr[2][1] has been changed.

I want to do is make part of a multidimensional array references to another array

English is not my native, I might misunderstand you...
I supposed, that you wanted to avoid coping of the array and desired to manipulate the part of the array elements via new reference.
What I want to do is make the second column of the multidimensional array references to a one dimensional array.
I hope, I've got the point.
The code I've proposed maps three successive elements multArr[2][0], multArr[2][1], multArr[2][2].
As elements multArr[0][2], multArr[1][2], multArr[2][2] are not successive and the difference inside memory between them equals to 3*sizeof(int) the common pointer arithmetic iterator used with arrays fails. Thus, I'am afraid to represent those elements as an alone one-dimensional array reference is not possible. You have to rearrange em, that means copy.
Or
You have to use some other representation against common arrays.

May it be possible for you to make up your mind to exchange 'cols' and 'rows'? Represent columns with the last index making column elements to be successive and array mapping to be possible.
Last edited on
Topic archived. No new replies allowed.