title: Strings in Structures in C
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c">What is the difference between char s[] and char *s in C?</a><br>
    <a href="https://stackoverflow.com/questions/12121759/difference-between-char-str-and-char-strn">Difference between char *str = “…” and char str[N] = “…”?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a structure defined as:</p>
  
  <pre><code>typedef struct
  {
    bool                  configured;
    bool                  active;
    uint32                lastComms;
    uint32                rxChRvdTime;
    char                  *name;
  }vehicle;
  </code></pre>
  
  <p>and I initialize it as follows:</p>
  
  <pre><code>static vehicle *myVehicle; 
  </code></pre>
  
  <p>When I want to initialize the name, I use:</p>
  
  <pre><code>myVehicle-&gt;name = "helloworld";
  </code></pre>
  
  <p>And this works fine. But when I wan't to set it to something other than a string literal, I seem to run into problems.</p>
  
  <pre><code>char *tmpName = "foobar";
  strcpy(myVehicle-&gt;name, tmpName);
  </code></pre>
  
  <p>So why doesn't strcpy work? Do I somehow need to preallocate the string size in the structure before hand?  Should I not be using a pointer for the 'name' field, since there can only be one vehicle?</p>