Are arduino structs like old or new C?

I'm new to arduino, but I'm an old hand at C and C++. Can somebody tell me if arduino structs are like new C (ie C++) where typedef isn't really needed as you can declare a struct object type by just using "struct foo {}; " and then later declare instances with just "foo fooInstance;"? Or must I mimic this by using typedef as a workaround, per old C?

Any chance there's "class" or "enum"?

Do I have to use pointers to pass structs around, or are they created equal with other variable types?

Curly brace struct initialization?

I tried to find some discussion of structs in the language reference. Oddly is wasn't there (the reference barely explains arrays). Maybe I was looking at the wrong language reference.
Lots of garbage websites that purport to explain arduino structs are at a sub-beginner level. I hope somebody can point me to the real stuff.

1 Like
  • The Arduino compiler handles C/C++.
1 Like

enum defaults to 16-bit values but, example

 enum EnumName : byte {IDLE, MIDDLE, END};

uses half as much RAM

IIRC there is or was an attribute that does or did that
edit: aha! what I typed was

__attribute__

That has me wondering if the default boolean can do that?
I still can't figure why the "optimizing" compiler defaults to 16 bits to hoid 1!

It was written for beginners and never have we gotten a complete reference. Since it's free, we still get more than our money's worth!

2 Likes

Both are fully supported.

Hello two_dee_man

Welcome to the world's best Arduino forum ever.

Take a view to get some ideas and to gain the knowledge.

https://www.learncpp.com/

Have a nice day and enjoy coding in C++.

BTW enjoy coding

The struct directive was introduced in C++ in order to be able to design user-defined data types and functions.

This directive is a powerful combination in conjunction with the array instruction.

I always like to use this combination for the abstraction of the programming task.

No typedef needed.

1 Like

The struct is not a directive; it is a keyword of C Language. It is the class keyword that was introduced in C++ by Bjarne Stroustrup (pronounced as) in 1983.

C had structs from the start AFAIK!

2 Likes

I love this forum, you can always learn something new here.

2 Likes

1. I am telling based on K&R's Legendary "The C Programming Language" Book:

(1) The struct is a keyword. It introduces/encaptulates a data structure of conglomerate type as declared below and enclosed within pair of braces:

struct 
{
     int x;
     int y;
     float z;
} ;

(2) An optinal name called structure tag (simply tag) may follow the keyword struct. Given below an example; where, point is a tag. The tag can be used subsequently as a shorthand to refer the declaration in braces of Step-1(1).

struct point
{
     int x;
     int y;
     float 
} ;

==>

struct point pt;

In the above, pt is a variable which is a structure of type "struct point". It is an instance of the structure.

The variable pt has never been uttered as an object in this Book of C, ; it is a concept introduced in C++ that encpsulates both "data and methods".

(3) In K&D's Book, I have not seen the structure declaration to contain the following access specifiers which are seen in "C++ Class Declaration".

private:
public:
protected:

(4) In C++, the tag is enough to refer the structure declaration; there is no need for the tage to follow the class keyword. In C, the tag follows the struct keyword as is seen in Step-1(2). For example:

class Rectangle
{
     private:
          int width;
         int length;

     public:
         Rectangle(int, int);
         void calArea();
};

Rectangle rect(4, 5);   //object has followed the Class Name = Calss Tag

.... to be continued.

1 Like

Because those are C++ specific. So I'm not surprised :wink:

2 Likes

And is why/how the following declartion compiled created using struct keyword?

struct Rectangle
{
  private:
    int width;
    int length;

  public:
    Recatangle(int, int);
    void calArea();
};

You can not compare what K&R wrote in e.g. their second edition book with a modern C++.

The difference between a modern C++ struct and a class is that all members of a struct are public by default and all members are private by default. And that is how far my knowledge goes.

1 Like

OK, thanks for all the helpful replies. What I'm gathering from the response is that the "arduino language" is C++. Is this correct to say? Or is it some C++ subset? For example, can I do operator overloading in arduino? It's weird that there's no reference. What's the compiler -- is it just gnu g++ or some such? If so, then all my questions are thereby answered. Or is it something specifically created for arduino. I know that CS people love to make new compilers for things. Was that done here, or is it just the gnu compiler with a glitzy IDE?

This might depend on the board. There are C++ things that are not supported out of the box.

Basically gnu-c / gnu-c++ tailored towards the board; e.g. for AVR based boards it's avr-gcc and avr-g++.

e.g.: ch552duino has C backend (sdcc), not C++ :slight_smile:

What are the advantags of using Arduino IDE Platform to develop applications for ATmega328P Microcontroller?

1. I am an Assembly Language Programmer. (I assume that) Arduino IDE Platform is not known to me. I am familiar with Microchip Studio. I can develop any application whatever I like and of course up to the level of my expertise.

2. Arduino IDE offers the following facilities:
(1) Complex data structures of the target MCU can be handled (at almost Natural Language level) using C++ Programming Language.

(2) Sensors Interfacing have been made easy by the manufacturers/third parties through the supply of Library Files/Functions which are compatible with C++ Language of the Arduino IDE.

(3) Devices Interfacing (WiFi, Graphics Display, etc.) have been made easy by the similar scheme of Step-2(2).

(4) An excellent platform for learning the syntax and semantics rules of C++ Language being able to see the outcomes of experiments and imaginations at hardware levels.

(5) And many more...

I have used sdcc (Small Device C Compiler) a lot for the 89S52 Microcntroller.

When compiled as C++ by default in our IDE.