1

I'm working on my Arduino project, which is the only C/C++ compiler I'm using. I'm stumbling on why I cannot use the struct as a type on my function parameter.

Not sure if this specific to Arduino compiler design, or general C/C++ programming.

struct myStruct_t {
    byte var1;
    byte var2;
    };

myStruct_t myStruct;

void setup() {

}

void loop() {

}


void myFunc(myStruct_t *myVar) {
    int i = 0;
}

This results in a compiler errors:

error: variable or field 'myFunc' declared void

error: 'myStruct_t' was not declared in this scope

error: 'myVar' was not declared in this scope

If I comment out the declaration of the "myFunc" then it compiles as is. I'm not clear on why I can declare and use a variable of that structure, however I can use the structure as a parameter type. Does the "struct" type not act as a type for parameter use?

Thanks.

Riccarr
  • 103
  • 8
  • 7
    [tag:c] or [tag:c++], pick one, because they treat this code significantly differently, enough so to cause the exact problems you're seeing. – Ben Voigt Mar 23 '15 at 00:54
  • 1
    I suggest reading at least the first answers to these questions: http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions and http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – Fabio says Reinstate Monica Mar 23 '15 at 00:55
  • The code is valid C++, the problem in essence is that the Arduino IDE is retarded, and causes errors like these in an attempt to hide some of C++'s complexity (see the linked answer for details). Personally, I ran away from it as soon as I could, opting for build systems that let me write actual C++. – Matteo Italia Mar 23 '15 at 01:11
  • 1
    There is no c/c++ it's c or c++. – Iharob Al Asimi Mar 23 '15 at 01:24
  • @MatteoItalia: Let's not use such despicable words please. – Lightness Races in Orbit Mar 23 '15 at 01:33
  • Thanks for pointing out the "already answered" post. I did do a search prior to posting my own; albeit appears I did not do a very thorough search. I also bastardized the language naming by saying "C/C++" simple because I didn't know which exactly the Arduino IDE compiler uses. Sorry to you purist. Nevertheless ... I got my answer here, so thanks all. Cheers! – Riccarr Mar 23 '15 at 21:40

1 Answers1

1

Because in c you need to typedef to achieve that, otherwise you need to use struct to refer to the structure.

I'd recommend against typedefing and also against the _t in the structure name, but if you want it that way just do this

typedef struct myStruct_t {
    byte var1;
    byte var2;
} myStruct_t;

and do not use global variables, pass variables as parameters.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    C compatibility is not the problem, code for the Arduino is C++ by default (C++11, IIRC). – Matteo Italia Mar 23 '15 at 01:13
  • @MatteoItalia I don't know that and that doesn't make any sense at all, that might be a compiler used in arduino, don't you think that it's logical to have a c compiler, and there is no such thing as c compatibility, it's either c or c++, whether you like it or not. – Iharob Al Asimi Mar 23 '15 at 01:21
  • 1
    I'll rephrase: OP is using the Arduino IDE/libraries, as from the code he posted (Arduino tag, no #include, setup/loop = that's an Arduino PDE "sketch"), and Arduino sketches are C++, period (Arduino libraries are written in C++, the Arduino IDE uses g++ from AVR-GCC). The problem is not "you have to write C because you are using a C compiler", because he's using a C++ compiler. The actual problem is "the Arduino IDE is retarded", see the linked answer. – Matteo Italia Mar 23 '15 at 01:28