Home > Enterprise >  Write values to a 3D array inside a struct in C
Write values to a 3D array inside a struct in C

Time:10-06

I've been writing a function in C that executes 2D convolutions, and for that I need to import to it three different 3D arrays. So I declared, for instance:

typedef struct {
    float img[6][6][1];
} input_6_t;

And now I need to write values into the struct. Later I will pass that as a pointer to the function that will do the convolution.

I tried:

input_6_t test_image;
test_image.img = { {1,2,3,4,5,6} , {2,1,1,1,2,2} , {3,1,1,1,2,2} , {4,1,1,1,2,2} , {5,2,2,2,2,2} , {6,2,2,2,2,2} };

But it returns a syntax error. What would be the proper way to do this?

Thank you in advance.

CodePudding user response:

You need to adjust initializer a bit:

input_224_t test_image = {
  .img = { {1,2,3,4,5,6} , ..., {6,2,2,2,2,2} }
};

Though the compilation it may flood you with warnings about missing braces around initializer.

To avoid it: Either add braces around each scalar (a bit cumbersome):

input_224_t test_image = {
  .img = { {{1},{2},{3},{4},{5},{6}} , ..., {{6},{2},{2},{2},{2},{2}} }
};

or drop the last [1] dimensions from type of img member:

typedef struct {
    float img[6][6];
} input_6_t;
  • Related