1

I want to create a structure like this:

 struct commands
{
  int   cmd;
  char  descr[25];
};

commands cmds[] =
{
  {16, "Hammond Organ"},
  {17, "Percussive Organ"},
  {18, "Rock Organ"},
  { 0, "" }                   // end of list marker 
};

The compiler doesn't protest, but in a simulator the values of cmds[i].descr are all zeros.

And the line

Serial.write(cmds[1].descr);

prints nothing.

Please recommend how to rewrite the code.

Thanks.

EDIT

Thanks for your answer. It helped me a lot.

There is something wrong with the simulator (Simulator for Arduino - version 1.11E Designed by Virtronics). In the meantime I received the Mega 2560 board and the code works well with the board.

3
  • 1
    What version of the IDE and avr-g++ are you using? If you enable verbose compilation, you should see the location of the avr-tools (avr-g++, avr-objdump...) and the generated elf file. Run avr-nm -Cn path_to_the_elf_file to see the memory address where cmds is stored, then avr-objdump -s -j .data path_to_the_elf_file to see its contents. Does it look as expected? – Edgar Bonet Dec 23 '19 at 10:15
  • Arduino IDE 1.8.10. As I am a beginner, I don't understand the rest of your question. – bernkopf48 Dec 23 '19 at 20:27
  • 1
    My issue is now resolved by the answers of VillageTech and VE7JRO. – bernkopf48 Dec 23 '19 at 20:33
2

There must be something wrong with the simulator you're using, because your code works for me (Arduino IDE 1.8.9, OSX, Arduino Uno).

struct commands
{
  int   cmd;
  char  descr[25];
};

commands cmds[] =
{
  {16, "Hammond Organ"},
  {17, "Percussive Organ"},
  {18, "Rock Organ"},
  { 0, "" }                   // end of list marker 
};

void setup(){
  Serial.begin(9600);
  Serial.write(cmds[1].descr);
}

void loop(){}
2
  • The above sketch produces no errors or warnings using my OS and IDE version. – VE7JRO Dec 23 '19 at 1:24
  • 1
    Same here. Compiles cleanly with -Os -Wall -Wextra and works perfectly with both avr-g++ 5.4.0 (shipped with Ubuntu 19.10) and 7.3.0 (shipped with the Arduino IDE 1.8.10). – Edgar Bonet Dec 23 '19 at 9:53
0

Try to use char pointer notation (char *) instead of array notation (char[]) in structure declaration:

struct commands
{
    int   cmd;
    char*  descr;
};
  • OR -

use struct declaration as-is, but change the array initialization like below:

commands cmds[] =
{
    {16, {"Hammond Organ"}},
    {17, {"Percussive Organ"}},
    {18, {"Rock Organ"}},
    { 0, {""}}                   // end of list marker 
};

Additionally, it seems that it always should be written as:

struct commands cmds[] = 
{
    // here your code
};

If you have disabled warnings in your compiler, enable them again - and try to write program the way that you will newer receive warnings.

Thanks for your answer. It helped me a lot.

There must be something wrong with the simulator (Simulator for Arduino - version 1.11E Designed by Virtronics). In the meantime I received the Mega 2560 board and both char pointer notation (char ) and array notation (char[]) work well with the board. The compiler (Arduino IDE v. 1.8.10) really produces warnings: ISO C++ forbids converting a string constant to 'char' [-Wwrite-strings]. Perhaps the simulator is more meticulous than the compiler and therefore refuses to work with a code thar generates warnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.