title: Fill a char array with int values
body: <p>I'm using a library for an Arduino project that has a function I need to call. This function accepts just one parameter of type <code>const char*</code>. Let's call it <strong>foo</strong>.</p>
  
  <p>I need to pass some <code>int</code> values to <strong>foo</strong>, so I convert them first using <code>sprintf</code>. So far so good.</p>
  
  <p>The problem comes when I try to fill an array with <code>int</code> values converted to <code>char</code> and then call <strong>foo</strong> with each of the values in the array.</p>
  
  <p>I hope this explains the problem better:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  
  // This function cannot be modified because
  // is a part of a library
  void foo(const char *bar){
      cout &lt;&lt; "Result: " &lt;&lt; bar &lt;&lt; endl;
  }
  
  int main() {
      char *values[10]; // My array of values
      char tmp[10]; // Temporary buffer for the int &gt; char conversion
  
      for(int i = 0; i &lt; 10; i++){
          int samplevalue = i * 2; // Just a sample value, not important
          sprintf(tmp, "%d", samplevalue); // Copy the sample value to the temporary buffer
          values[i] = tmp; // Assign the value of the temp var to a position in my values array
          cout &lt;&lt; values[i] &lt;&lt; endl;
      }
  
      cout &lt;&lt; "==============" &lt;&lt; endl;
  
  
      // Here comes the problem:
      for(int i = 0; i &lt; 10; i++){
          foo(values[i]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The output of that code is the following:</p>
  
  <pre><code>0
  2
  4
  6
  8
  10
  12
  14
  16
  18
  ==============
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  Result: 18
  </code></pre>
  
  <p>As you can see, all of the <strong>Result</strong> lines are equal to the last value assigned to the <code>tmp</code> var. I guess it's because each of the values in the <code>values[10]</code> array contains a pointer to the tmp variable instead of its actual value.</p>
  
  <p>What I'd like to have is a different number on each <strong>Result</strong> line, as in the first <code>for</code> loop.</p>
  
  <p>I guess it's pretty obvious that I'm not even near to be a C++ expert and any help will be highly appreciated.</p>
  
  <p>Thanks!</p>