Home > Mobile >  Structure of a book in C
Structure of a book in C

Time:10-19

I am new to C programming and in the development of this exercise I encountered this error that I cannot resolve:

Fields must have a constant size: 'variable length array in structure' extension will never be supported

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

int main(int argc, const char * argv[]) {
    int nChapters = 2;
    
    typedef struct {
        char title[50];
        char author[50];
    } Heading;
    
    typedef struct {
        char title[50];
        int number_pages;
    } Chapter;
    
    typedef struct {
        Heading heading;
        Chapter chapters[nChapters]; //Fields must have a constant size: 'variable length array in structure' extension will never be supported
    } Book;
    
    printf("\n");
    system("read -p 'Press enter to continue...' ");
    printf("Hello, World!\n");
    return 0;
}

If I replace chapters[nChapters] with an int like chapters[2], program run without problems. Thanks in advance!

CodePudding user response:

In C you have to declare arrays using a fixed length, your nChapters variable is indeed, a variable. You can turn it into a constant variable by simply adding the const keyword:

const int nChapters = 2

CodePudding user response:

You can use the preprocessor directive #define:

#define nChapters 2

CodePudding user response:

The issue is that you are assuming that it is obvious

Chapter chapters[nChapters];

that value of nChapters is 2. It works that way for a array which is not within a struct or a union. This is supported by weird, non-standard, non-GCC (but accepted as an extension by GCC in C90 onwards), not recommended feature called as VLA or Variable Length Arrays. Using these, one can allocate a auto class array.

Referring to GNU/GCC standard, section 6.20, It is trivial to note that,

The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.

C99 recommends a better way to deal with this requirement - by using flexible length array.

§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. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

So, that would change your struct to:

typedef struct {
    Heading heading;
    Chapter chapters[]; 
} Book;

And then allocate the memory dynamically from heap - using malloc.

CodePudding user response:

The size of the array member of the struct has to be a constant expression (skip "flexible member" case and GCC's VLA-in-struct extension).

In the C standard the only portable way to have a true named integer constant is using enums. Just replace:

int nChapters = 2;

with this:

enum { nChapters = 2 };
  •  Tags:  
  • c
  • Related