1

I was trying to figure out how can I create sub arrays from within a larger array and got a piece of code here and started using it.

I created an array of ints

int arr[10];
for(int h=0;h<10;h++)
{
    arr[h]=20+h;
}

Now say I want a sub-array (of 4 ints) within the same larger array

int (&arrOnly4Elements)[4]=(int (&)[4])(*arr);

It works well and does what I want. While I understand references and that they point to actual objects, What I am not able to understand how the above code works. why do we need the braces to surround &arrOnly4Elements Also, can anyone explain me the RHS (int (&)[4])(*arr); in detail step by step manner.

10
  • The new standard way to do this is to use std::span Commented May 12, 2020 at 19:00
  • 2
    The code seems to violate the strict aliasing rule (which causes UB), so it doesn't really "work". Commented May 12, 2020 at 19:01
  • @NathanOliver Thanks..Will try that out.. but i want to understand how the above works. Commented May 12, 2020 at 19:01
  • @HolyBlackCat: It works seemlessly. behaves as an array of 4 ints. I keep playing with it. In case what I understood means "work" Commented May 12, 2020 at 19:02
  • 1
    @RohitGaneshan Undefined behavior can have any effect, including not giving you any errors, or blowing up in your face randomly. Even if some code appears to work, it doesn't necessarily mean that it's valid. "explain me the RHS (int (&)[4])(*arr); in detail" It's a C-style cast of *arr to type int (&)[4] (which is "a reference to an array of 4 ints"). Commented May 12, 2020 at 19:04

1 Answer 1

1

cdecl.org translates it for you:

int (&arrrOnly4Elements)[4]: declare arrrOnly4Elements as reference to array 4 of int

int &arrrOnly4Elements[4]: declare arrrOnly4Elements as array 4 of reference to int

As NathanOliver pointed out, C++20 introduces std::span. You should take a look at it (also compare this SO question). A std::span is a templated view into an array/contiguous sequence of objects. It consists of a pointer and a size. It makes accessing arrays and sub arrays convenient (allows range based for) and safe (keeps track of the size).

int arr[10];
std::span<int> arr_span = arr;
std::span<int,4> arr_subspan1 = arr_span.first<4>();
std::span<int> arr_subspan2 = arr_span.first(4);

If you cannot yet switch to C++20 you might consider checking GSL which provides a gsl::span which was lately aligned to match std::span.

Sign up to request clarification or add additional context in comments.

5 Comments

@RohitGaneshan Not directly for one struct (makes no sense there), but for arrays of structs. I'll explain more in my answer.
Yes i meant array of structs basically
Let me provide some more details. Actually i was doing the same thing with a 2-dimensional array of structs. I set offsets for both the dimensions after creating a sub array(2-dimensional) and use those offsets while creating the next sub array. So in my question it can be *(arrr+offset). I guess all these can be handled by gsl:span. In case you would like to see exact code, i can paste after a couple of hours. Just in case I am not clear enough
@RohitGaneshan If you have another question, feel free to post another question. If you change your question once it is answered that makes it hard to give a final answer.
Will do. In the meantime I will try out the span

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.