title: Referring an array inside a C structure
body: <p>I come from Java background and trying to understand C structures, pointers and arrays better. Here's the sample code that I am playing with: </p>
  
  <p>If the following works: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      char string[] = "Hello";
      printf("%c",string[0]);
      return 0;
  }
  </code></pre>
  
  <p>Why does the following return with an error?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      typedef struct{
          int x;
          char string[8];
      }ST_DATA;
  
      ST_DATA *my_data;
      my_data-&gt;x = 100;
      my_data-&gt;string = "Hello"; // issues a warning, described below
  
      printf("%d",my_data-&gt;x); // works fine
      printf("%c",my_data-&gt;string[0]);
      return 0;
  }
  </code></pre>
  
  <p>Following is the error that I am getting:</p>
  
  <p>Compilation error            time: 0 memory: 2292 signal:0</p>
  
  <p>prog.c: In function ‘main’:</p>
  
  <p>prog.c:12:18: error: incompatible types when assigning to type ‘char[8]’ from type ‘char *’</p>
  
  <p>my_data->string = "Hello";</p>
  
  <p>I tried with the following changes as well:</p>
  
  <p><strong>a)</strong></p>
  
  <pre><code>my_data-&gt;string[] = "Hello";
  </code></pre>
  
  <p>This will give me the following error:</p>
  
  <p>prog.c: In function ‘main’:</p>
  
  <p>prog.c:12:18: error: expected expression before ‘]’ token</p>
  
  <p>my_data->string[] = "Hello";</p>
  
  <pre><code>              ^
  </code></pre>
  
  <p><strong>b)</strong></p>
  
  <pre><code>my_data-&gt;string[8] = "Hello";
  </code></pre>
  
  <p>This returns with a runtime error. Presumably, the error occurs when I am printing the first character.</p>
  
  <p>There must be something stupid that I am doing or expecting (being used to coding with other languages than C), but I can't seem to figure out why this is happening and how to get it to work. I'd greatly appreciate any <em>pointers</em> (ha! get it?)</p>