# Compiler flaw, probably deliberate, infuriating

**URL:** <https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637>\
**Category:** IDE 2.x\
**Created:** [July 10, 2023, 7:27pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637 "2023-07-10T19:27:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottmayo](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@scottmayo](https://forum.arduino.cc/u/scottmayo)\
**Post date:** [July 10, 2023, 7:27pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/1 "2023-07-10T19:27:21Z")

</div>

The C++ compiler in the Arduino IDE, which I assume is some fork of g++, is a worthy compiler and it supports a lot of modern C++ - lambdas and all sorts of things.

So it's really weird that it's been crippled to reject something that's been part of C from day one.

You cannot pass a pointer to a user defined struct or class to a function. It refuses to recognize the underlying type. Ditto references to structs or the structs themselves. Ditto any user defined types at all, even freaking enums.

This can only be deliberate, and I'm guessing it was done as a lazy hack to prevent inexperienced programmers from passing entire structs to functions, which could rapidly get painful on small processors like arduinos. But in the attempt to Save US All From Ourselves, they blocked all references and pointers to those types as well, and that's unacceptable.

This really makes a mess of some very simple, basic programming techniques, especially involving state machines, which is a lot of what arduinos are for.

I don't need workarounds. I know plenty of ways around this, and they're all poor style or inefficient. If the compiler author wants to turn passing structs into a warning or even disallow it, great. It's not that hard to add to that the compiler. But preventing things as fundamental as passing a ref or pointer to a struct makes simple coding harder and it doesn't actually help anyone.

Fixing this is years overdue.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 10, 2023, 8:03pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/3 "2023-07-10T20:03:34Z")

</div>

> [@scottmayo](#):
>
> You cannot pass a pointer to a user defined struct or class to a function. It refuses to recognize the underlying type. Ditto references to structs or the structs themselves. Ditto any user defined types at all, even freaking enums.

You are probably doing something wrong. Show us an example

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [July 10, 2023, 8:11pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/4 "2023-07-10T20:11:10Z")

</div>

Works for me. You are describing the problem wrong or trying to do something different than what you describe. Give a minimal example.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 10, 2023, 8:51pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/5 "2023-07-10T20:51:04Z")

</div>

> [@scottmayo](#):
>
> passing entire structs to functions

@gcjr often demonstrates structure usage... find his posts by clicking his profile activity.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 10, 2023, 8:56pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/6 "2023-07-10T20:56:35Z")

</div>

> [@scottmayo](#):
>
> You cannot pass a pointer to a user defined struct or class to a function.

I quite often pass pointers to structs, and can't recall ever having a problem.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 10, 2023, 10:46pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/7 "2023-07-10T22:46:32Z")

</div>

> [@scottmayo](#):
>
> You cannot pass a pointer to a user defined struct or class to a function.

does this demonstrate what you're trying to do

```arduino
struct Timer {
    const byte Pin;
    unsigned long msecPeriod;
    unsigned long msecLst;
};

Timer timers [] = {
    { 13, 1000 },
    { 12, 700 },
    { 11, 200 },
    { 10, 500 },
};
const int Ntimer = sizeof (timers) / sizeof(Timer);

unsigned long msec;

// -----------------------------------------------------------------------------
void
timerChk (
    Timer *t )
{
    if (msec - t->msecLst >= t->msecPeriod) {
        t->msecLst = msec;
        digitalWrite (t->Pin, ! digitalRead (t->Pin));
    }
}

// -----------------------------------------------------------------------------
void loop()
{
    msec = millis ();
    Timer *t = timers;
    for (unsigned n = 0; n < Ntimer; n++, t++)
        timerChk (t);
}

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin(115200);

    Timer *t = timers;
    for (unsigned n = 0; n < Ntimer; n++, t++)
        pinMode (t->Pin, OUTPUT);
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 10, 2023, 11:12pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/9 "2023-07-10T23:12:52Z")

</div>

> [@Delta\_G](#):
>
> I think the issue you have is with what the IDE does to the file BEFORE it goes to the compiler. It creates function prototypes for you and that is where the problem occurs because it puts that prototype before your class or struct definition.

This is most likely the cause of your problem, I have run into the situation a few times and inserting a function prototype at an appropriate location fixed it.

---

<div class="post-metadata">

**Author:** ![scottmayo](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@scottmayo](https://forum.arduino.cc/u/scottmayo)\
**Post date:** [July 10, 2023, 11:53pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/10 "2023-07-10T23:53:33Z")

</div>

Oh for pity's sake. Is there a way to turn off the automatic generation of prototypes? I'm an experienced C++ programmer and I define (or declare) my functions before they are used, so I absolutely don't need the IDE adding incorrect crap to my already well formed code. If it's going to try to dumb down C++ so people don't have to create forward references, it should at least figure out when they are not needed. Barring that there need to be an Off switch.

That's really annoying.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 10, 2023, 11:56pm UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/11 "2023-07-10T23:56:43Z")

</div>

> [@scottmayo](#):
>
> Is there a way to turn off the automatic generation of prototypes?

Generate them yourself, or use a professional IDE. Or use one of the fixes already presented.

The Arduino IDE is _free software_, intended for teaching and learning, and making life a little less complicated for beginners.

The phrase "probably deliberate" in the post title is a **ridiculous** accusation and does not engender sympathy.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 11, 2023, 12:31am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/12 "2023-07-11T00:31:27Z")

</div>

> [@scottmayo](#):
>
> Oh for pity's sake. Is there a way to turn off the automatic generation of prototypes?

what's wrong with the code a posted? isn't you're similarly structured?

can you post your code

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 11, 2023, 12:44am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/13 "2023-07-11T00:44:18Z")

</div>

> [@scottmayo](#):
>
> Is there a way to turn off the automatic generation of prototypes?

If you provide a function prototype, the IDE will not attempt to generate a prototype for the function. Not sure if the automatic prototype generation can be entirely disabled.

You can also provide your own main() function, and do away with the necessity of having setup() and loop().

---

<div class="post-metadata">

**Author:** ![scottmayo](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@scottmayo](https://forum.arduino.cc/u/scottmayo)\
**Post date:** [July 11, 2023, 12:50am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/15 "2023-07-11T00:50:45Z")

</div>

Actually, no. This IS deliberate behavior on the part of the IDE, and it's not making things better. It's teaching people bad C++ - in C++, you either define or declare everything before use. The IDE is trying to make that important aspect of the language go away in the name of "simplification" - and then hassling people who know the actual rules, by helpfully doing something wrong.

Imagine someone starting out coding with an Arduino and then trying to move C++ professionally, and getting confused about all this talk of forward references. That's a disservice.

I've been using C++ since the cfront days, so I'm not a typical arduino hobbyist. If there's a better IDE out there, I'd be willing to pay for it.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 11, 2023, 12:59am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/17 "2023-07-11T00:59:41Z")

</div>

is it any better if you put that code in a .cpp file and call it from the .ino?

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 11, 2023, 1:15am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/18 "2023-07-11T01:15:57Z")

</div>

> [@scottmayo](#):
>
> This IS deliberate behavior on the part of the IDE

Do keep digging that pit you are in! Bye.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 11, 2023, 6:16am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/19 "2023-07-11T06:16:51Z")

</div>

> [@Delta\_G](#):
>
> If you don't want to play with baby toys, then don't play with baby toys. But don't grab baby toys and then complain that they aren't grown up enough for you

+1  
I think that sums it up.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [July 11, 2023, 11:01am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/20 "2023-07-11T11:01:57Z")

</div>

I haven't gone looking, but this thread begs the question - is there a 'graduate course' for our learners, that takes them from the handholding(childhood) of the IDE to a stand-alone 'grown up'(perhaps, someone should have used a slightly different, less negative, phrasing...) world?  
Just asking, as it would seem some here believe there are two worlds - Arduino 'baby', and 'C++' adult, which would seem to suggest there's room for a 'training wheels' stage.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 11, 2023, 11:22am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/21 "2023-07-11T11:22:56Z")

</div>

What I don't understand is that if you are defining the functions before they are being used then why do you have a problem ?

Have you provided an example of a sketch that illustrates your problem ?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 11, 2023, 11:32am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/22 "2023-07-11T11:32:04Z")

</div>

@UKHeliBob

You can try the example in [Incorrect placement of auto-generated prototypes gives misleading error messages · Issue #362 · arduino/arduino-builder · GitHub](https://github.com/arduino/arduino-builder/issues/362)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 11, 2023, 11:40am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/23 "2023-07-11T11:40:53Z")

</div>

interesting report raised in Nov 2020

comment:

> We'll investigate soon and report back

reported as 'preprocessing type: imperfection' end of sept 2021

and mid 2023 there is no change (still fails in IDE v2)

I guess we can all live with an imperfect world

side note: reordering it a bit solves the issue

```arduino
// Arduino IDE 1.8.13

enum class StateA { one, two, three } ;
enum class StateB { one, two, three } ;

StateA stateA ;
StateB stateB ;

void setStateA( StateA newState ) { stateA = newState ;}
void setStateB( StateB newState ) {stateB = newState ;}

void setup() {
  setStateA( StateA::one ) ;
  setStateB( StateB::one ) ;
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 7, 2024, 11:41am UTC](https://forum.arduino.cc/t/compiler-flaw-probably-deliberate-infuriating/1146637/24 "2024-01-07T11:41:27Z")

</div>

Closed on January 7, 2024, 11:41am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
