Say I have a struct
defined like so:
typedef struct MyStruct {
double a[3];
double b[6];
} MyStruct;
I pass the structures to a function to perform some operations. For example:
void MyFcn(MyStruct *out, const MyStruct *in) {
out->a[2] = in->b[5];
/* do more stuff */
}
If I want to qualify the array pointers MyStruct.a
and MyStruct.b
as not having overlapping memory with the restrict keyword, is there any way of doing this?
Perhaps most compilers would optimize assuming that MyStruct.a
and MyStruct.b
point to contiguous memory block without any aliasing anyways, and there's no point in adding the restrict qualifier? I have no idea.
I know I could simply make a and b pointers instead of arrays like so
typedef struct MyStruct {
double *restrict a;
double *restrict b;
} MyStruct;
but considering the size of the arrays are known, this approach makes it difficult to debug overflows and unnecessarily complicates variable initializations (requires dynamic memory allocations or compound literals and designated initializers).
EDIT
From the comments below, I need to clarify that I intended for the example to better describe the question, not constrain it to that use case. The answers did clarify that struct members cannot have overlapping memory (thats' what unions are for).
However, the question still remains for different function inputs/outputs. For any number of function inputs/outputs with different struct definitions, would the compiler optimize if there are possibilities of aliased arrays between structs? If it would not, how to give the restrict keyword?