I have a problem where I need to handle small slices of a big array of doubles.
Is there a way to define a pointer which lets me handle it as a small array?
Given:
double bigarr[10000] = {0.0};
// doing something to fill the array
double *smallarr[10] = &bigarr[297];
int i;
for (i=0; i<10; i++) {
*smallarr[i] += 15.0;
}
This example should result in adding 15.0
to the elements from bigarr[297]
..bigarr[306]
(but this is just an example. I want to do something more complicated)
What's the easiest way to achieve this?
Is there a way to define a pointer which lets me handle it as a small array?
A plain double *
will serve just fine:
double *smallarr = &bigarr[297];
or
double *smallarr = bigarr + 297;
You can then use smallarr
much like you would bigarr
, with smallarr[0]
equivalent to bigarr[297]
.
On the other hand, your original
double *smallarr[10] = &bigarr[297];
is an array of pointers, not an array of double
nor a pointer to an array. You can define a pointer to an array:
double (*smallarr_ptr)[10] = (double (*)[10]) &bigarr[297];
... but the syntax for using that is not the same as the syntax for using bigarr
. And you don't gain much from encoding the sub-array length into the pointer type, especially if it's constant.