People who code: we want your input. Take the Survey

Hot answers tagged

18

As taken from the accepted answer from When should you use a class vs a struct in C++? The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and ...


11

Unlike C, an instance of a struct in C++ is an object in exactly the same way as an instance of a class. From the point-of-view of the compiled code, they are identical. Memory usage, alignment, access times etc. are exactly the same (i.e. there are no overheads). From the programmer's point-of-view, there is a very minor difference. Members of a struct ...


8

led h_red = {0,0,255,0,300}; Here, you are defining a variable, and at the same time giving it an initial value. This is called an initialization. led leds[LEDS]; Here you are defining an array. Since it is in global scope, and not explicitly initialized, it is implicitly initialized to all bytes zero. leds[0] = {0,0,0,0,0}; Here, you are trying to give ...


6

An easy way of accessing any type of data in PROGMEM is to use this small library: PROGMEM_readAnything.h #include <Arduino.h> // for type definitions template <typename T> void PROGMEM_readAnything (const T * sce, T& dest) { memcpy_P (&dest, sce, sizeof (T)); } template <typename T> T PROGMEM_getAnything (const T * sce) ...


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 ...


5

In plain C, structs are identified by "struc tags", which do not live in the same namespace as type names. Thus, if you declare struct RGB { ... }; a variable of this type should be declared as struct RGB led; In order to avoid the inconvenience of repeatedly typing struct, a common idiom is to define it as a type: typedef struct { ... } RGB; ...


5

This is a bug in the Arduino IDE: when converting your .ino file to a .cpp file, it added a declaration for the function ledFactory() before the declaration of the type led_t. As it appears to be adding those declarations right before the first function of your sketch, a simple workaround is to put all your type definitions before your functions.


4

As other answers have pointed out, your particular struct and class are indistinguishable performance wise (There are slight differences in the scopes of the type names, due to the way you defined your struct). The delineation in C++ is not between struct and class, but between types that are POD (plain old data) and types that are not, as explained in this ...


4

I tried the code from the link you posted and it worked: struct Gyro_data_structure { char command_name[6]; int gyro_X; int gyro_Y; int gyro_Z; }; struct Gyro_data_structure Gyro_data = {"Hello", 48, 49 , 50}; int size_gyro = sizeof(struct Gyro_data_structure); void setup() { Serial.begin(9600); // opens serial port, sets data rate to ...


3

typedef void (*MessageHandler)(void); // def messagehandler as pointer to func struct AvcInMessage { MessageHandler msgHandler; byte dataSize; byte data[8]; char description[20]; } InMessage[] = { { function1, <datasize>, {<byte0>, <byte1>, ..., }, "DescriptionHere" }, // etc, for ...


3

The typedef is no longer needed, using the 'struct' with a name declares the type. // Arduino Uno struct buttons { int color[3]; int positions[4]; char init[20]; void (*fn)(); }; // Declare the functions here, or use prototyping void func1(); void func2(); buttons test[] = { { {0,0,0}, {0,0,100,100}, "getSomething", func1 }, { {40,40,40}...


3

Consider the following: void printBook(const books &theBook) { Serial.println("Name of the book is: "); Serial.println(theBook.name); Serial.println("author is: "); Serial.println(theBook.author); Serial.println("ID is: "); Serial.println(theBook.ID); } void loop() { printBook(MyBook); printBook(YourBook); } You should ...


3

In terms of memory, what difference will this make? None. Structs and classes are the same thing, differing only in protection levels, and instantiating either creates an 'object'. still see the struct technique being used heavily - why is this? Less typing if you're not trying to hide data members.


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 ...


3

My method makes use of the __FlashStringHelper class: // For convenience: typedef const __FlashStringHelper *FlashString; FlashString reverseEnum(int val) { switch (val) { case 0: return F("Option zero"); case 1: return F("Option one"); case 2: return F("Option two"); default: return F("Invalid Enum"); } } Printing ...


3

First of all, you have to decide whether you want to send the data as raw bytes (“binary data”) or as an ASCII text representation. Binary tends to be more efficient: you can send a float with full precision in only four bytes, whereas you would typically need 8 to 9 significant digits to recover the full precision from an ASCII representation. Binary is, ...


3

To make it generic, you simply divide the sizeof the array by the sizeof the type: for(int i=0; i<sizeof(controls)/sizeof(SwitchControl); i++){ } Then you can later change the items if the array, or the structure and the code will be the same.


2

Using it with heterogeneous types is trickier, since it has to be copied by hand instead of using one of the pgm_read_*() macros. SimulatorRecord PendingRecord; memcpy_P(&PendingRecord, &SimulatorQueue[_iDataPos], sizeof(PendingRecord));


2

If the second isn't working, it may be best to use the other 2. Especially on the Arduino with limited RAM, you will not have enough if you pass a full struct to a function. Passing a pointer is less memory-intensive, and if it works, use it!


2

Note that typedef struct { ... } Foo; is obsolete in C++ and you should just use struct Foo { ... }; It's not very clear in the answers but the other effect of structs is public inheritance by default vs private inheritance by default with classes. As mentioned by others, using struct is generally a convention for POD types. Also technically 'struct vs ...


2

Modified my struct by changing the first two elements from float to int. struct weather { int tempData; int humData; float pressData; } weather wData; Everything transmits fine now. I don't really need the precision of a float for temperature and humidity. However, I would still like to find the problem. Could there be some problem with the size of ...


2

First off, consider trying '\032' or '\x1A' for the ctrl-z. (See, eg, How to send ctrl+z in C on stackoverflow.) To send a ctrl-z, use something like: Keyboard.press(ctrlKey); Keyboard.press('z'); delay(100); Keyboard.releaseAll(); For more examples of using modifier keys, see the KeyboardPress page at arduino.cc, and the example program KeyboardLogout....


2

You are not specifying a size for your readings array: unsigned int readings[][28]; That is not saying "Give me an array of an unknown number of chunks, each of 28 values", it is actually saying "Give me a pointer to a block of memory (which I will provide later) which is arranged in rows of 28 values". In C you must either provide a size for an array, in ...


2

From the specs: The first byte in each 32-bit USB-MIDI Event Packet is a Packet Header contains a Cable Number (4 bits) followed by a Code Index Number (4 bits). [...] [...] The Cable Number (CN) is a value ranging from 0x0 to 0xF indicating the number assignment of the Embedded MIDI Jack associated with the endpoint that is transferring the ...


2

Are you trying to use the same function for different arrays at different times, or are you trying to use a function that handles four different arrays at the same time? If the former, you'd write something like void myfunc( T *arr, size_t size ) // where T is the type of the array { for ( size_t i = 0; i < size; i++ ) arr[i] = some_new_value(); } ...


2

No, it is not repeated. Unless you are using templates, each method of each class is stored only once in the compiled program. Or even not stored at all if the optimizer notices it is not used. Just to demystify the classes a bit, you can consider them as a kind of “syntactic sugar” over an “object oriented” programming pattern. The following C++ code: ...


2

ArduinoJson stores strings differently depending on their types: for const char*, it saves a pointer; for other types (char*, String...) it copies the string. Because ArduinoJson stores the copy of the string in the JsonDocument, you must increase the capacity of the JsonDocument. In your case, you pass Peripheral::current_state, which is a String, so ...


2

A more generic way (usable in future cases) is: // number of items in an array #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) Now you can use ARRAY_SIZE (the_array) in your code like this: for(int i=0; i<ARRAY_SIZE (controls); i++){ } The above works in C as well as C++. The more C++ way of doing it is to use a template like this: // number of ...


1

No, the code is not repeated for each instance. Instance methods are like functions. There is only one copy of the code. There's a dispatch table that maps method calls to their implementations. (Actually, that's standard C++. I don't know for sure how it's implemented in the Arduino C++ compiler)


Only top voted, non community-wiki answers of a minimum length are eligible