title: Which method is correct for Initializing a wchar_t string?
body: <p>I am writing a program and I need to initialize a message buffer which will hold text. I am able to make it work, however I am writing below various ways used to initialize the strings in C and I want to understand the difference. Also, which is the most appropriate method for initializing a <code>wchar_t</code>/<code>char</code> string?</p>
  <p><strong>Method I:</strong></p>
  <pre><code>wchar_t message[100];
  </code></pre>
  <p>based on my understanding, this will allocate a memory space of 200 bytes (I think size of <code>wchar_t</code> is 2 bytes on Windows OS). This memory allocation is static and it will be allocated inside the .data section of the executable at the time of compiling.</p>
  <p>message is also a memory address itself that points to the first character of the string.</p>
  <p>This method of initializing a string works good for me.</p>
  <p><strong>Method II:</strong></p>
  <pre><code>wchar_t *message;
  message=(wchar_t *) malloc(sizeof(wchar_t) * 100);
  </code></pre>
  <p>This method will first initialize the variable message as a pointer to <code>wchar_t</code>. It is an array of wide characters.</p>
  <p>next, it will dynamically allocate memory for this string. I think I have written the syntax for it correctly.</p>
  <p>When I use this method in my program, it does not read the text after the space in a string.</p>
  <pre><code>Example text: &quot;This is a message&quot;
  </code></pre>
  <p>It will read only &quot;This&quot; into the variable message and no text after that.</p>
  <p><strong>Method III:</strong></p>
  <pre><code>wchar_t *message[100];
  </code></pre>
  <p>This will define message as an array of 100 wide characters and a pointer to <code>wchar_t</code>. This method of initializing message works good. However, I am not sure if it is the right way. Because message in itself is pointing to the first character in the string. So, initializing it with the size, is it correct?</p>
  <p>I wanted to understand it in more depth, the correct way of initializing a string. This same concept can be extended to a string of characters as well.</p>