Skip to main content
12 votes

Same structure taking different memory space on Uno and NodeMCU ESP8266, leading to data corruption when transferring via nRF24L01+

int, long, etc have different sizes depended on the compiler and target. Use explicit sizes to make sure you're variables have the size you want. Eg. uint8_t, int16_t or int32_t, ... (as @Mat ...
Swedgin's user avatar
  • 241
12 votes
Accepted

Same structure taking different memory space on Uno and NodeMCU ESP8266, leading to data corruption when transferring via nRF24L01+

There are three main issues you may need to deal with when passing structures between systems that use different processors. The first is that data types vary in size between architectures. You can ...
Peter Green's user avatar
10 votes
Accepted

Initializing Array of structs

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. ...
Edgar Bonet's user avatar
  • 44.3k
9 votes

Same structure taking different memory space on Uno and NodeMCU ESP8266, leading to data corruption when transferring via nRF24L01+

In addition to the rule of always using fixed and predictable sizes across different architectures, it's also a good idea to pack your structs. This prevents the compiler from padding smaller ...
Majenko's user avatar
  • 106k
6 votes
Accepted

Own type definition using a struct does not name a type

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 ...
Edgar Bonet's user avatar
  • 44.3k
5 votes
Accepted

Why does a variable size struct not compile in the Arduino IDE?

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-...
AnT stands with Russia's user avatar
5 votes
Accepted

Send structure through Serial

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 ...
Fahad's user avatar
  • 544
4 votes

Typedef struct not working

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 ...
Edgar Bonet's user avatar
  • 44.3k
3 votes

How to get the sizeof array of structs

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, ...
Eugenio Pace's user avatar
3 votes
Accepted

Message Interpreter and Handler - How to store a function name in a struct?

typedef void (*MessageHandler)(void); // def messagehandler as pointer to func struct AvcInMessage { MessageHandler msgHandler; byte dataSize; byte data[8]; char ...
JRobert's user avatar
  • 15.3k
3 votes

How do I implement reverse enumeration?

My method makes use of the __FlashStringHelper class: // For convenience: typedef const __FlashStringHelper *FlashString; FlashString reverseEnum(int val) { switch (val) { case 0: return ...
Majenko's user avatar
  • 106k
3 votes
Accepted

Doest a local struct variable get destroyed?

As described in the comments: Your local struct variable probably always is placed in the same space in memory and this part of the memory isn't overwritten by your program. When declaring local ...
chrisl's user avatar
  • 16.5k
3 votes
Accepted

How do I convert a structure to a char[] for use in Wire.h, or is there a better way?

There is an overload of Wire.write that lets you send an arbitrary array of bytes: virtual size_t write(const uint8_t *, size_t); In order to use it, you have to cast the address of your structure to ...
Edgar Bonet's user avatar
  • 44.3k
2 votes
Accepted

Use of struct and sizeof in C for Arduino

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(...
Edgar Bonet's user avatar
  • 44.3k
2 votes
Accepted

Help with struct variable

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)(); }; // ...
Jot's user avatar
  • 3,246
2 votes

Unexpected results and hard crash when modifying and addressing arrays & variables within a struct

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 "...
Majenko's user avatar
  • 106k
2 votes
Accepted

MIDIUSB - Why is the command put twice?

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 ...
Ignacio Vazquez-Abrams's user avatar
2 votes
Accepted

Function, struct or class?

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 ...
John Bode's user avatar
  • 136
2 votes

Why does a variable size struct not compile in the Arduino IDE?

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 ...
KIIV's user avatar
  • 4,777
2 votes

How to get the sizeof array of structs

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(...
Nick Gammon's user avatar
  • 38.7k
2 votes

Send structure through Serial

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 ...
Edgar Bonet's user avatar
  • 44.3k
2 votes
Accepted

Second item in array not gets assigned from struct object, ArduinoJson

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 ...
Benoit Blanchon's user avatar
2 votes

Same structure taking different memory space on Uno and NodeMCU ESP8266, leading to data corruption when transferring via nRF24L01+

The esp8266 has a 32 bit mcu and the uno an 8 bit mcu so their variables have different storage requirements. You need to allow for this in your code.
Bra1n's user avatar
  • 1,028
2 votes

Same structure taking different memory space on Uno and NodeMCU ESP8266, leading to data corruption when transferring via nRF24L01+

For all the reasons mentioned in the other answers (different types sizes, alignment requirements and endianness), it's often a bad idea to use structs for data that needs to be sent/received (be it ...
jcaron's user avatar
  • 131
1 vote
Accepted

Accessing struct's variable parametrically on ESP8266

You can create a 'hardcoded' copy function: void CopyParameter(int parameterIndex) { switch (parameterIndex) { case 0: final_par.ext_inputs = def_par.ext_inputs; break; case 1: ...
Michel Keijzers's user avatar
1 vote

Accessing struct's variable parametrically on ESP8266

You can do it but it can be awkward. You'll need an array of structs, each member of which describes one member of your 'struct parameters' - essentially, an expansion of your present 'keys[]'. Then ...
JRobert's user avatar
  • 15.3k
1 vote

Typedef struct not working

There are 2 errors: you are returning a class type, and not the temp object RGB func(){ RGB temp; return RGB; --> return temp; } To use your typedef you should put the struct "name" ...
fabianoriccardi's user avatar
1 vote

How do I implement reverse enumeration?

I'm no expert, but I imagine something along these lines would work: char* day(int day) { static char* dayName[7] = {"Sunday", "Monday", .... }; if (day > 0 && day < 8) { ...
Jim Mack's user avatar
  • 245
1 vote

Memory size issue with the struct

In your screen shots you are using string literals that are waaaay longer than in your sample code. I suspect using PROGMEM could be a way to work around. But why bothering with constant strings and ...
Kwasmich's user avatar
  • 1,513

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