-1

I wrote a piece of C code as below:-

typedef struct {
    unsigned int buffer_ctrl[4];
    unsigned int buffer1[10];
    unsigned int buffer2[40];
    unsigned int buffer3[20];
    unsigned int buffer4[15];
    unsigned int *buffer_ptr[4] = {buffer1, buffer2, buffer3, buffer4};
    unsigned int canary[4];
} buffer_t;

I wrote this in a header file which I included in a main code. I had read up a lot of examples on jagged array in C and thought this would work just fine. One of the links was Do jagged arrays exist in C/C++?.

However, when I compile I get the error "expected ';' at end of declaration lsit". Can someone please help explain what might be the error here? Thanks!

1 Answers1

2

You cannot assign to buffer_ptr within the definition of the buffer_t structure itself. You have to define a variable of the type buffer_t first and then assign to it.

You can do something like this:

buffer_t bt = {
    .buffer_ptr[0] = bt.buffer1,
    .buffer_ptr[1] = bt.buffer2,
    .buffer_ptr[2] = bt.buffer3,
    .buffer_ptr[3] = bt.buffer4
};
P.W
  • 26,289
  • 6
  • 39
  • 76
  • There generally isn't a good reason to permanently store an array that is so easy to calculate, though. You can create such an array when you need it, and save memory when you don't. – yyny Nov 13 '18 at 18:55