I want to convert this piece of code using this memory layout transformation : https://software.intel.com/en-us/articles/memory-layout-transformations (Array of Structures to Structure of Arrays)
Here are the codes before and after applying the transformation and I want to know whether I'm doing it right or not?

Before:

typedef struct {
  double x;
}str1;

typedef struct {
  str1 v;
}str2;

int main(){

str2 *pointer;

double y = pointer[1].v.x;

return 0;
}

After:

typedef struct {
  double x[1];
}str1;

typedef struct {
  str1 v;
}str2;

int main(){

str2 pointer;

double y = pointer.v.x[1];

return 0;
}

You've got the right idea. There are typo's in your example though.

Thanks, I fixed the typo's. Is it still correct if instead of double x[1], I use double *x ?

Well, you'll need to point *xto somwhere to use it, since it's not allocated for you,

For example: x = (double *)malloc(ARRAY_SIZE*sizeof(double));. Remember to free heap allocated memory when you're done with it.

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.