title: Work around error 'Address of stack memory associated with local variable returned'
body: <p>I am trying to call a method that will generate a <strong>2D char array</strong> (array of strings) and return it to be used in another function.</p>
  
  <p>My example:</p>
  
  <pre><code>char ** example(void)
  {
      char *test[3];
  
      int i;
      for (i = 0; i &lt; 3; i++) {
          test[i] = malloc(3 * sizeof(char));
      }
  
      test[foo][bar] = 'baz'; // of course I would declare 'foo' and 'bar'
      // ...
      // ...
  
      return test;
  }
  </code></pre>
  
  <p>And then I would like to be able to use the array as follows:</p>
  
  <pre><code>void otherMethod(void)
  {
      char ** args = example();
      // do stuff with args
  }
  </code></pre>
  
  <p>The problem is that this produces the <strong>error</strong>:</p>
  
  <blockquote>
    <p>warning: address of stack memory associated with local variable 'test' returned
          [-Wreturn-stack-address]</p>
  </blockquote>
  
  <p>I could solve this problem by defining <code>test</code> in the global scope as opposed to locally, but I would very much rather not do this as it seems messy, especially if I am to have several of these.</p>
  
  <p><strong>Is there a way to create and return an array of strings in C without defining it globally?</strong></p>