1

Is something like this possible in C? It would be really nice to have both of these.

    typedef struct {
        int r;
        char a0[0];
        char a1[0];
    }

8
  • 5
    No, that is not possible. Also, flexible array members should be declared with [], not [0]. If you need to have multiple arrays of lengths determined at run-time “in” a structure, use pointers (and set esch to point to memory allocated for its array). Commented Jan 31, 2022 at 1:49
  • In the c array members cannot be declared and initialised together. They are done separately. Commented Jan 31, 2022 at 2:04
  • @EricPostpischil [0] is a GCC extension. Commented Jan 31, 2022 at 2:58
  • See the C standard §6.7.2.1 Structure and union specifiers ¶18: As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. …. Hence, only one flexible array member. Commented Jan 31, 2022 at 3:06
  • Also critical 6.7.2.1 Structure and union specifiers(p3) "... the last member of a structure with more than one named member may have incomplete array type; such a structure ... shall not be a member of a structure or an element of an array. " A flexible array member brings with it significant limitations on how that struct may be used. Commented Jan 31, 2022 at 6:06

3 Answers 3

1

No. Rather use dynamically allocated memory. Something like:

typedef struct {
    int *data1;
    size_t len1

    int *data;
    size_t len2;
} sometype;

sometype *alloc_sometype(size_t len1, size_t len2) {
    sometype *n = malloc(sizeof(sometype));

    if (!n) return NULL;

    n->len1 = len1;
    n->len2 = len2;

    n->data1 = malloc(sizeof(int) * len1);
    n->data2 = malloc(sizeof(int) * len2);

    // Error handling if those malloc calls fail

    return n;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This looks like a case for 2 objects of the same struct type, wrapped in another struct.
1

The flexible array must be the last member in the struct so you can't have two. However, if both arrays are supposed to have the same size, you could combine them:

#include <stdio.h>
#include <stdlib.h>

typedef struct { // a struct to keep one a0 a1 pair
    char a0;
    char a1;
} A;

typedef struct {
    size_t len;
    A a[];        // flexible array of a0 a1 pairs
} data;

data *data_create(size_t len) {
    data *d = malloc(sizeof *d + len * sizeof *d->a);
    if(d) d->len = len;
    return d;
}

int main() {
    data *d = data_create(10);

    for(size_t i = 0; i < d->len; ++i) {
        d->a[i].a0 = i;
        d->a[i].a1 = i+1;
    }
    
    free(d);
}

Comments

-1

C99 compilation will allow this, but some strict compiler throw error. Now a1 never be used, because a0 is itself variable length. If you want to divide data into two part, first read into a single array then use pointer forwarding to read data into two part.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.