title: C: Returning string from function
body: <p>I have a function that returns a string:</p>
  
  <pre><code>const *char getMyPassword()
  {
      return "mysecretpassword";
  }
  </code></pre>
  
  <p>Well, it worked perfectly, but I discovered that if I would run "strings" on Unix systems it shows up in the list.. not good. </p>
  
  <p>What is the easiest possible way to replace it? The function sits in a library and I want to keep it flexible. I now started mallocing within the function and the string in strings disappeared. However, when would I free it again?</p>
  
  <pre><code>char * getMyPassword()
  {
  unsigned char arr[] = { 'p', 'a', 's', 's', 'w', 'o', 'r' , 'd', '\0' };
  char *return_arr = malloc(sizeof(arr));
  strcpy(return_arr, arr);
  return return_arr;
  
  }
  </code></pre>
  
  <p>If I was to malloc before and pass a pointer, then how could I have known the size before as the passwords size is only known within the function?</p>
  
  <p>As a plan B I could pass a huge array, but that seems not very elegant. How should I approach this?</p>
  
  <p><strong>EDIT: I added the strcpy(return_arr, arr). I actually had it in the original code, but forgot it in here.</strong></p>