Why does a variable size struct not compile in the Arduino IDE?Why can't the new Arduino IDE 1.6.7 compile extern “C”?How can Arduino know that the number in a variable is a pin number and not something else?Advice for checking integrity of serial char strings?Arduino: put string through variable in arrayHow does Arduino IDE 'Get Board Info'?Transfer serial data to struct variable in ArduinoArduino not adding decimals correctly on double variableWhy does this array have stored values in it even though I have not put any values in it?Arduino OTA port not updating in Arduino IDEGlobal Variable does not Change when Value is set within Boolean Function

What can cause an unfrozen indoor copper drain pipe to crack?

Would encrypting a database protect against a compromised admin account?

What food production methods would allow a metropolis like New York to become self sufficient

Why should password hash verification be time constant?

Was Mohammed the most popular first name for boys born in Berlin in 2018?

Renting a house to a graduate student in my department

Can more than one creature benefit from multiple Hunter's Mark spells cast on the same target?

Why do the Avengers care about returning these items in Endgame?

No such column 'DeveloperName' on entity 'RecordType' after Summer '19 release on sandbox

Is it nonsense to say B -> [A -> B]?

Detect the first rising edge of 3 input signals

What do "KAL." and "A.S." stand for in this inscription?

Why use steam instead of just hot air?

How to select certain lines (n, n+4, n+8, n+12...) from the file?

Does the 500 feet falling cap apply per fall, or per turn?

Why can't I prove summation identities without guessing?

How to efficiently lower your karma

Ex-manager wants to stay in touch, I don't want to

Is ‘despite that’ right?

Exception propagation: When to catch exceptions?

Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?

Why was the ancient one so hesitant to teach Dr Strange the art of sorcery

Is there a need for better software for writers?

My perfect evil overlord plan... or is it?



Why does a variable size struct not compile in the Arduino IDE?


Why can't the new Arduino IDE 1.6.7 compile extern “C”?How can Arduino know that the number in a variable is a pin number and not something else?Advice for checking integrity of serial char strings?Arduino: put string through variable in arrayHow does Arduino IDE 'Get Board Info'?Transfer serial data to struct variable in ArduinoArduino not adding decimals correctly on double variableWhy does this array have stored values in it even though I have not put any values in it?Arduino OTA port not updating in Arduino IDEGlobal Variable does not Change when Value is set within Boolean Function













1















This sketch does not compile in the Arduino IDE



void setup() 
// put your setup code here, to run once:



struct test
int i;
char variable[];
;

typedef struct test test;

test t =
0, "hi"
;

void loop()
// put your main code here, to run repeatedly:




Arduino throws



sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]

};

^

exit status 1
initializer-string for array of chars is too long [-fpermissive]


However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?










share|improve this question









New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 1





    It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

    – Jot
    10 hours ago











  • @Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

    – AnT
    9 hours ago












  • @AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

    – Jot
    9 hours ago











  • @Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

    – AnT
    9 hours ago
















1















This sketch does not compile in the Arduino IDE



void setup() 
// put your setup code here, to run once:



struct test
int i;
char variable[];
;

typedef struct test test;

test t =
0, "hi"
;

void loop()
// put your main code here, to run repeatedly:




Arduino throws



sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]

};

^

exit status 1
initializer-string for array of chars is too long [-fpermissive]


However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?










share|improve this question









New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 1





    It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

    – Jot
    10 hours ago











  • @Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

    – AnT
    9 hours ago












  • @AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

    – Jot
    9 hours ago











  • @Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

    – AnT
    9 hours ago














1












1








1








This sketch does not compile in the Arduino IDE



void setup() 
// put your setup code here, to run once:



struct test
int i;
char variable[];
;

typedef struct test test;

test t =
0, "hi"
;

void loop()
// put your main code here, to run repeatedly:




Arduino throws



sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]

};

^

exit status 1
initializer-string for array of chars is too long [-fpermissive]


However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?










share|improve this question









New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











This sketch does not compile in the Arduino IDE



void setup() 
// put your setup code here, to run once:



struct test
int i;
char variable[];
;

typedef struct test test;

test t =
0, "hi"
;

void loop()
// put your main code here, to run repeatedly:




Arduino throws



sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]

};

^

exit status 1
initializer-string for array of chars is too long [-fpermissive]


However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?







programming array struct






share|improve this question









New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 7 hours ago









Glorfindel

4331512




4331512






New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 10 hours ago









the_architectthe_architect

1062




1062




New contributor



the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




the_architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









  • 1





    It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

    – Jot
    10 hours ago











  • @Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

    – AnT
    9 hours ago












  • @AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

    – Jot
    9 hours ago











  • @Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

    – AnT
    9 hours ago













  • 1





    It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

    – Jot
    10 hours ago











  • @Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

    – AnT
    9 hours ago












  • @AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

    – Jot
    9 hours ago











  • @Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

    – AnT
    9 hours ago








1




1





It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

– Jot
10 hours ago





It hurts to see that code, so it must be wrong. A struct is a definition of a variable (wich contains other variables). How would you create an array of those structs when they have different lengths?

– Jot
10 hours ago













@Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

– AnT
9 hours ago






@Jot: The struct type itself is declared correctly. There's nothing wrong with an [] array as the last member of a struct. However, in standard C language the only way to make such structs to "have different lengths" is to malloc them individually. This is what this [] feature is designed for. In all other contexts the [] array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.

– AnT
9 hours ago














@AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

– Jot
9 hours ago





@AnT I know that it is set to zero size and I have read your answer with the correct type definition and declaration of the structs. the_architect tries to put the characters into the struct itself. That is not possible.

– Jot
9 hours ago













@Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

– AnT
9 hours ago






@Jot Not sure what answer you are talking about. Placing the characters into the struct itself, as the OP is trying to, is possible through GNU-specific extension. As I said already, the OP code is perfectly correct from GNU point of view.

– AnT
9 hours ago











3 Answers
3






active

oldest

votes


















3














Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).



That's because size of struct must be known compile time and if you provide char variable[]; you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.



Maybe you're looking for something like this:



struct test 
int a;
const char * ptr;
;

test paramx 1, "some string";
test paramy 2, "another string";

test array[] = 3,"three", 4, "four", 5,"five";





share|improve this answer

























  • char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

    – AnT
    9 hours ago







  • 1





    And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

    – KIIV
    9 hours ago












  • Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

    – AnT
    9 hours ago












  • Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

    – KIIV
    9 hours ago












  • The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

    – AnT
    9 hours ago



















3














Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.



However, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.



If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.



This is why you were able to compile it with your standalone g++ compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.



It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a .c file. Or just specify the array size explicitly.






share|improve this answer
































    -1














    What the range voltage to supply arduino?






    share|improve this answer








    New contributor



    Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.



















    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

      – VE7JRO
      7 hours ago











    Your Answer






    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("schematics", function ()
    StackExchange.schematics.init();
    );
    , "cicuitlab");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "540"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    the_architect is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65267%2fwhy-does-a-variable-size-struct-not-compile-in-the-arduino-ide%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).



    That's because size of struct must be known compile time and if you provide char variable[]; you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.



    Maybe you're looking for something like this:



    struct test 
    int a;
    const char * ptr;
    ;

    test paramx 1, "some string";
    test paramy 2, "another string";

    test array[] = 3,"three", 4, "four", 5,"five";





    share|improve this answer

























    • char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

      – AnT
      9 hours ago







    • 1





      And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

      – KIIV
      9 hours ago












    • Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

      – AnT
      9 hours ago












    • Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

      – KIIV
      9 hours ago












    • The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

      – AnT
      9 hours ago
















    3














    Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).



    That's because size of struct must be known compile time and if you provide char variable[]; you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.



    Maybe you're looking for something like this:



    struct test 
    int a;
    const char * ptr;
    ;

    test paramx 1, "some string";
    test paramy 2, "another string";

    test array[] = 3,"three", 4, "four", 5,"five";





    share|improve this answer

























    • char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

      – AnT
      9 hours ago







    • 1





      And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

      – KIIV
      9 hours ago












    • Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

      – AnT
      9 hours ago












    • Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

      – KIIV
      9 hours ago












    • The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

      – AnT
      9 hours ago














    3












    3








    3







    Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).



    That's because size of struct must be known compile time and if you provide char variable[]; you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.



    Maybe you're looking for something like this:



    struct test 
    int a;
    const char * ptr;
    ;

    test paramx 1, "some string";
    test paramy 2, "another string";

    test array[] = 3,"three", 4, "four", 5,"five";





    share|improve this answer















    Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).



    That's because size of struct must be known compile time and if you provide char variable[]; you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.



    Maybe you're looking for something like this:



    struct test 
    int a;
    const char * ptr;
    ;

    test paramx 1, "some string";
    test paramy 2, "another string";

    test array[] = 3,"three", 4, "four", 5,"five";






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 9 hours ago

























    answered 10 hours ago









    KIIVKIIV

    3,7321617




    3,7321617












    • char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

      – AnT
      9 hours ago







    • 1





      And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

      – KIIV
      9 hours ago












    • Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

      – AnT
      9 hours ago












    • Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

      – KIIV
      9 hours ago












    • The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

      – AnT
      9 hours ago


















    • char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

      – AnT
      9 hours ago







    • 1





      And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

      – KIIV
      9 hours ago












    • Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

      – AnT
      9 hours ago












    • Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

      – KIIV
      9 hours ago












    • The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

      – AnT
      9 hours ago

















    char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

    – AnT
    9 hours ago






    char variable[]; is not a zero-sized array. It is a flexible array. In standard C its size depends on how much memory was allocated by the user for the whole struct object. And in extended GNU-C the extra memory can be requested the way the OP requests it: by using an initializer. As far as GNU-C is concerned, the code is perfectly fine.

    – AnT
    9 hours ago





    1




    1





    And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

    – KIIV
    9 hours ago






    And it works like it's zero size array pointing after the end of struct, so you have to allocate more than sizeof(struct) to use it without "buffer overflow". But as it was noted, it can't work on statically allocated struct instance. And in C++ it has to be struct containing POD's only.

    – KIIV
    9 hours ago














    Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

    – AnT
    9 hours ago






    Well, it can't work for non-dynamic struct object in pedantic standard C. But the OP is apparently not trying to use pedantic standard C. And yes, it can and will work in extended GNU-C (and in GNU-C++ after version 6 of the compiler). The original code is perfectly fine as far as GNU-extended versions of these languages are concerned. The code will compile and work as intended in Arduino IDE once it is placed into a .c file, as I clearly stated in my answer. Once Arduino IDE moves to a later version of AVR-GCC, this code will compile and work as intended right away.

    – AnT
    9 hours ago














    Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

    – KIIV
    9 hours ago






    Hower I'm not sure he really wanted to use FAM. It might be just coincidence. Maybe he wanted just const char * variable; That should be perfectly legal in this case (or flash string helpers / progmem pointers)

    – KIIV
    9 hours ago














    The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

    – AnT
    9 hours ago






    The title of the question seems to suggest that the OP did actually want to use a variable-size struct. Whether they meant it literally, as a reference to this specific language feature - I don't know.

    – AnT
    9 hours ago












    3














    Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.



    However, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.



    If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.



    This is why you were able to compile it with your standalone g++ compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.



    It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a .c file. Or just specify the array size explicitly.






    share|improve this answer





























      3














      Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.



      However, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.



      If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.



      This is why you were able to compile it with your standalone g++ compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.



      It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a .c file. Or just specify the array size explicitly.






      share|improve this answer



























        3












        3








        3







        Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.



        However, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.



        If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.



        This is why you were able to compile it with your standalone g++ compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.



        It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a .c file. Or just specify the array size explicitly.






        share|improve this answer















        Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.



        However, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.



        If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.



        This is why you were able to compile it with your standalone g++ compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.



        It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a .c file. Or just specify the array size explicitly.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 5 hours ago

























        answered 10 hours ago









        AnTAnT

        4088




        4088





















            -1














            What the range voltage to supply arduino?






            share|improve this answer








            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.



















            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

              – VE7JRO
              7 hours ago















            -1














            What the range voltage to supply arduino?






            share|improve this answer








            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.



















            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

              – VE7JRO
              7 hours ago













            -1












            -1








            -1







            What the range voltage to supply arduino?






            share|improve this answer








            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            What the range voltage to supply arduino?







            share|improve this answer








            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            share|improve this answer



            share|improve this answer






            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            answered 8 hours ago









            Mechatronics clubMechatronics club

            1




            1




            New contributor



            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




            New contributor




            Mechatronics club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

              – VE7JRO
              7 hours ago

















            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

              – VE7JRO
              7 hours ago
















            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

            – VE7JRO
            7 hours ago





            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

            – VE7JRO
            7 hours ago










            the_architect is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            the_architect is a new contributor. Be nice, and check out our Code of Conduct.












            the_architect is a new contributor. Be nice, and check out our Code of Conduct.











            the_architect is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Arduino Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65267%2fwhy-does-a-variable-size-struct-not-compile-in-the-arduino-ide%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Log på Navigationsmenu

            Wonderful Copenhagen (sang) Eksterne henvisninger | NavigationsmenurSide på frankloesser.comWonderful Copenhagen

            Detroit Tigers Spis treści Historia | Skład zespołu | Sukcesy | Członkowie Baseball Hall of Fame | Zastrzeżone numery | Przypisy | Menu nawigacyjneEncyclopedia of Detroit - Detroit TigersTigers Stadium, Detroit, MITigers Timeline 1900sDetroit Tigers Team History & EncyclopediaTigers Timeline 1910s1935 World Series1945 World Series1945 World Series1984 World SeriesComerica Park, Detroit, MI2006 World Series2012 World SeriesDetroit Tigers 40-Man RosterDetroit Tigers Coaching StaffTigers Hall of FamersTigers Retired Numberse