I kept going with my example, and this is what I came up with - I'm sure that you can get something out of this
Code:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define BUFF 10
/****************************************************************
* Function get_subarr (get sub array)
*
* Returns a pointer to an array (located in a larger array)
*
* param m_buff (main buffer) - The buffer that the sub array will be
* allocated from.
*
* param m_buf_size (main buffer size) - The size of the m_buff array
*
* param idx (index)
* This is where the function will try to
* allocate the sub array
*
* param s_arr_size (sub array size)
* This is the size of the sub array
****************************************************************/
int (*get_subarr (int m_buf[], size_t m_buf_size, int idx, size_t s_arr_size) )[];
int main(void)
{
int main_buffer[BUFF] = {0,1,2,3,4,5,6,7,8,9};
int (*sub_buffer_ptr)[5] = NULL;
int i;
int sub_array_size = 4; //Play around with these two values...
int sub_array_index = 4; //
sub_buffer_ptr = get_subarr(main_buffer, BUFF, sub_array_index, sub_array_size);
if (sub_buffer_ptr == NULL)
{
fputs("Error in get_subarr", stderr);
return EXIT_FAILURE;
}
for(i=0;i<sub_array_size;i++)
{
printf ("%d ", (*sub_buffer_ptr)[i]);
}
return EXIT_SUCCESS;
}
int (*get_subarr (int m_buf[], size_t m_buf_size, int idx, size_t s_arr_size) )[]
{
if ((s_arr_size+idx) > m_buf_size)
{
return NULL;
}
else
{
return (int (*)[]) &m_buf[idx];
}
}