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

Is this homebrew "Cactus Grenade" cantrip balanced?

Is keeping the forking link on a true fork necessary (Github/GPL)?

Finding all files with a given extension whose base name is the name of the parent directory

If I arrive in the UK, and then head to mainland Europe, does my Schengen visa 90 day limit start when I arrived in the UK, or mainland Europe?

Why did Jon Snow admit his fault in S08E06?

Gravitational Force Between Numbers

Can you still travel to America on the ESTA waiver program if you have been to Iran in transit?

Cardio work for Muay Thai fighters

Who knighted this Game of Thrones character?

Freedom of Speech and Assembly in China

How to politely tell someone they did not hit reply all in email?

Natural Armour and Weapons

Why sampling a periodic signal doesn't yield a periodic discrete signal?

Must a warlock replace spells with new spells of exactly their Pact Magic spell slot level?

Creating second map without labels using QGIS?

How to let other coworkers know that I don't share my coworker's political views?

Is it legal to have an abortion in another state or abroad?

...And they were stumped for a long time

What did the 'turbo' button actually do?

Can a UK national work as a paid shop assistant in the USA?

Is there a simple example that empirical evidence is misleading?

Did this character show any indication of wanting to rule before S8E6?

What weight should be given to writers groups critiques?

The disk image is 497GB smaller than the target device



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













2















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



















  • 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
    May 9 at 20:02











  • @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
    May 9 at 20:18












  • @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
    May 9 at 20:50











  • @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
    May 9 at 20:55
















2















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



















  • 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
    May 9 at 20:02











  • @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
    May 9 at 20:18












  • @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
    May 9 at 20:50











  • @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
    May 9 at 20:55














2












2








2


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
















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















share|improve this question













share|improve this question




share|improve this question








edited May 9 at 22:07









Glorfindel

4561613




4561613










asked May 9 at 19:31









the_architectthe_architect

1134




1134







  • 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
    May 9 at 20:02











  • @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
    May 9 at 20:18












  • @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
    May 9 at 20:50











  • @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
    May 9 at 20:55













  • 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
    May 9 at 20:02











  • @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
    May 9 at 20:18












  • @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
    May 9 at 20:50











  • @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
    May 9 at 20:55








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
May 9 at 20:02





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
May 9 at 20:02













@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
May 9 at 20:18






@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
May 9 at 20:18














@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
May 9 at 20:50





@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
May 9 at 20:50













@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
May 9 at 20:55






@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
May 9 at 20:55











2 Answers
2






active

oldest

votes


















5














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

























  • Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

    – Jot
    May 11 at 1:13






  • 1





    @Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

    – AnT
    May 11 at 1:26



















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
    May 9 at 20:13







  • 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
    May 9 at 20:17












  • 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
    May 9 at 20:21












  • 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
    May 9 at 20:30












  • 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
    May 9 at 21:01












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
);



);













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

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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

























  • Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

    – Jot
    May 11 at 1:13






  • 1





    @Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

    – AnT
    May 11 at 1:26
















5














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

























  • Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

    – Jot
    May 11 at 1:13






  • 1





    @Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

    – AnT
    May 11 at 1:26














5












5








5







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 May 10 at 0:51

























answered May 9 at 19:56









AnTAnT

55219




55219












  • Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

    – Jot
    May 11 at 1:13






  • 1





    @Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

    – AnT
    May 11 at 1:26


















  • Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

    – Jot
    May 11 at 1:13






  • 1





    @Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

    – AnT
    May 11 at 1:26

















Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

– Jot
May 11 at 1:13





Thanks for the link to godbolt. I tried a few things and it goes wrong very fast. For example adding a variable to the struct after the flexible array, or with an array of structs. Some things work with gcc version 6, but not with 8 or 9. The sizeof() does not include the flexible array member. And so on.

– Jot
May 11 at 1:13




1




1





@Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

– AnT
May 11 at 1:26






@Jot: Flexible array member is naturally supposed to be the last field of the struct. Yes, sizeof does not take it into account. Flexible array members are intended to provide support for the good old "struct hack" (c-faq.com/struct/structhack.html) and the fact that sizeof ignores this member makes it easier to calculate the proper size for malloc.

– AnT
May 11 at 1:26












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
    May 9 at 20:13







  • 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
    May 9 at 20:17












  • 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
    May 9 at 20:21












  • 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
    May 9 at 20:30












  • 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
    May 9 at 21:01
















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
    May 9 at 20:13







  • 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
    May 9 at 20:17












  • 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
    May 9 at 20:21












  • 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
    May 9 at 20:30












  • 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
    May 9 at 21:01














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 May 9 at 20:35

























answered May 9 at 19:52









KIIVKIIV

3,7321718




3,7321718












  • 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
    May 9 at 20:13







  • 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
    May 9 at 20:17












  • 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
    May 9 at 20:21












  • 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
    May 9 at 20:30












  • 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
    May 9 at 21:01


















  • 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
    May 9 at 20:13







  • 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
    May 9 at 20:17












  • 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
    May 9 at 20:21












  • 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
    May 9 at 20:30












  • 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
    May 9 at 21:01

















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
May 9 at 20:13






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
May 9 at 20:13





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
May 9 at 20:17






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
May 9 at 20:17














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
May 9 at 20:21






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
May 9 at 20:21














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
May 9 at 20:30






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
May 9 at 20:30














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
May 9 at 21:01






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
May 9 at 21:01


















draft saved

draft discarded
















































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

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020