0

I have a byte array serialized from a stream char* Buf which points to an array of 64 bytes.

I wish to cast it into a function input parameter Foo(char[4][16] Buf) without copying each and every single bytes.

Any easy for doing so?

s k
  • 2,280
  • 3
  • 28
  • 39
  • Anything "difficult" you tried so far? – Susmit Agrawal Jan 26 '20 at 06:53
  • You could pass it to the function if the underlying data structure is the same. That can be done because you can represent a 2D contiguous data structure as contiguous 1D array. If not you have to iterate through it by calculating the exact positions- – Andre Kampling Jan 26 '20 at 06:59
  • 2
    Related: https://stackoverflow.com/questions/55593936/c-multidimensional-array-in-existing-memory – Michael Chourdakis Jan 26 '20 at 07:18
  • If you received that data from a file or via network, copying the 64 bytes is not going to impact the performance of your program noticeably. – Ulrich Eckhardt Jan 26 '20 at 07:54

2 Answers2

0

You can cast any buffer to any array you want:

void Foo(char(&buf)[4][16])
{
}

int main()
{
    char* buf = new char[64];
    Foo((char(&)[4][16])(buf));
    delete[] buf;
    return 0;
}
Ali Asadpoor
  • 317
  • 1
  • 12
0

I found the answer in link given by Michael Chourdakis

*reinterpret_cast<char(*)[4][16]>(Buf)
s k
  • 2,280
  • 3
  • 28
  • 39