title: Reading strings from structures in PROGMEM using a loop
body: <p><strong>Found a solution, see my comment below!</strong></p>
  
  <p>I am trying to read data which is placed in structures and stored in the flash memory of an Arduino Mega (ATmega 2560) using PROGMEM. The structure objects <code>manufacturer_1</code> and <code>manufacturer_2</code> are accessed with <code>pointers</code>.</p>
  
  <p>Due to size of the sketch; I decided to create a (relative) small example which illustrates the problem. The following code shows how I define the structures and data.</p>
  
  <pre><code>typedef struct
  {
     char info[20];
  } manufacturer_def;
  
  typedef struct
  {
     unsigned int totalManufacturers;
     const manufacturer_def* manufacturer[2];
  } data_def;
  
  const manufacturer_def manufacturer_1 PROGMEM =
  {
     "Manufacturer 1"
  };
  
  const manufacturer_def manufacturer_2 PROGMEM =
  {
     "Manufacturer 2"
  };
  
  const data_def data PROGMEM =
  {
    2,
    {
      &amp;manufacturer_1,
      &amp;manufacturer_2
    }
  };
  
  void setup() 
  {
    // Serial monitor setup
    Serial.begin(115200);   // Begin serial monitor
  }
  
  void loop() 
  {
     mainMenu();
  }
  </code></pre>
  
  <p><strong>The problem!</strong> </p>
  
  <p>I would like to fill an array with strings using a loop. The following code is not working properly:</p>
  
  <pre><code>void mainMenu()
  {
     unsigned int i = 0;
  
     unsigned int totalMenuItems = pgm_read_word(&amp;data.totalManufacturers);
     String menuItems[totalMenuItems];
  
     char str_buf[20];
  
     // Create array with items for menu
     for (i = 0; i &lt; totalMenuItems; i++)
     {
       strcpy_P(str_buf, data.manufacturer[i]-&gt;info);
       menuItems[i] = str_buf;
       Serial.println(menuItems[i]);
     }
   }
  </code></pre>
  
  <p>Output (section):</p>
  
  <pre><code>p�
  
  
  
  
  
  
  
  
  
  p�
  </code></pre>
  
  <p>Strangely, when I place the <code>strcpy_P</code> command outside the loop and specify the iteration variable by hand it works:</p>
  
  <pre><code>void mainMenu()
  {
    unsigned int i = 0;
  
    unsigned int totalMenuItems = pgm_read_word(&amp;data.totalManufacturers);
    String menuItems[totalMenuItems];
  
    char str_buf[20];
  
    strcpy_P(str_buf, data.manufacturer[0]-&gt;info);
    menuItems[0] = str_buf;
    strcpy_P(str_buf, data.manufacturer[1]-&gt;info);
    menuItems[1] = str_buf;
  
    // Create array with items for menu
    for (i = 0; i &lt; totalMenuItems; i++)
    {
      Serial.println(menuItems[i]);
    }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Manufacturer 1
  Manufacturer 2
  </code></pre>
  
  <p>Why is this happening?</p>