title: Return a string from function to main
body: <p>I want to return a string from a function (in the example <em>funzione</em>) to main. How to do this? Thank you!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define SIZE (10)
  
  /* TODO*/ funzione (void)
  {
      char stringFUNC[SIZE];
  
      strcpy (stringFUNC, "Example");
  
      return /* TODO*/;
  }
  
  int main()
  {
      char stringMAIN[SIZE];
  
      /* TODO*/
  
      return 0;
  }
  </code></pre>
  
  <p><strong>[EDITED]</strong> For those who need it, the complete version of the previous code (but without <em>stringMAIN</em>) is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define SIZE (10)
  
  char *funzione (void)
  {
      char *stringa = malloc(SIZE);
      strcpy (stringa, "Example");
  
      return stringa;
  } 
  
  int main()
  {
      char *ptr = funzione();
  
      printf ("%s\n", ptr);
  
      free (ptr);
  
      return 0;
  }
  </code></pre>