I need to treat a 1D-RAM buffer as a 2D array of a different type than what was declared, and have questions on different methods and what provides best performance.

Questions:
1. How to set things up so I can access an element by something like "ptr[x][y]"?
2. Would any particular method be faster than another?

I have a buffer that is declared for writing as

unsigned char buffer[FIELD_NUM][FIELD_SIZE];

Where each field is a 240x720 array (FIELD_SIZE=240*720). For reading from the buffer, a field is treated as a 240x360 array of unsigned short.

I can think of one way to "map" the buffer as an array of unsigned short:

One is to something like
unsigned short* ptr = &buffer[0][0];
for (i=0; i < 240; i++)
for (j=0; j < 360; j++)
ptr[j + i*360]

Would it be better to set up indexing so I could do something like
ptr = &buffer[0][0], so that I could access the 2D array by ptr[j]?
How do I set up ptr? (So, far no luck with what I *think* should work)

Appreciate any feedback!!

This should get ptr pointing at the first element of buffer.

unsigned short* ptr = (unsigned short*)buffer;

Please note, when you increment ptr(or access an element with an index ptr), the address increments by sizeof(unsigned short) and not by sizeof(unsigned char).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.