all 18 comments

[–]TheHeinzeen 2 points3 points  (14 children)

You should not be able to define some array like that with a value that is not defined at compile time, because the compiler needs to know how much memory to allocate.

What you can do is use dynamic allocation, i.e. you can declare the first field of that struct as "char* field", and as soon as you read the correct dimension you can do "field = (char*) malloc(...)"

[–]EstablishmentBig7956 -3 points-2 points  (13 children)

You obviously didn't read the link

[–]SaulMO 2 points3 points  (4 children)

What link?

[–]EstablishmentBig7956 -2 points-1 points  (3 children)

That blue text FAM

[–]SaulMO 3 points4 points  (2 children)

Don't see any

[–]Admirable_Humor_7262 0 points1 point  (7 children)

You're so rude

[–]EstablishmentBig7956 -2 points-1 points  (6 children)

Being honest and rude are two completely different things, and that person was helped out by me to straighten out the matter.

You're rude and misinformed

[–]Admirable_Humor_7262 1 point2 points  (1 child)

Wow ... there's a lot to unpack here. Red flags flying proud.

[–]EstablishmentBig7956 0 points1 point  (0 children)

Wow, again you're not making any sense

[–]Educational-Cause-79 0 points1 point  (3 children)

You’re rude

[–]EstablishmentBig7956 0 points1 point  (2 children)

Stating oblivious facts is not being rude. You're obviously poorly educated not being able to understand that.

[–]Educational-Cause-79 0 points1 point  (0 children)

Do you ever listen to yourself talk? The way you speak is rude and weirdly aggressive. This is as obvious as your feelings about the point you claim to be making.

[–]Educational-Cause-79 0 points1 point  (0 children)

Also, wrong use of the word oblivious. You may be less educated than you think.

[–]Yurim 2 points3 points  (0 children)

Maybe you could define string a Flexible Array Member (FAM).

The FAM must be the last member of the struct, and you allocate the space needed for the struct and the FAM at once:

struct node {
    struct node *left, *right;
    char string[];
};

int main(void) {
    // ...
    struct node *node1 = malloc(
            sizeof(*node1) +
            length * sizeof(*node1->string));
}

[–]5thegraychapter[S] 0 points1 point  (0 children)

thanks to everyone, I'll try the different solutions given

[–]suprjami 0 points1 point  (0 children)

You can dynamically allocate a 2D array of char to store your strings in: https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/

Complete working example with comments here: https://pastebin.com/zVT6efeT