title: Accessing array declared by malloc
body: <p>I was working on an assignment (details in <a href="https://stackoverflow.com/questions/12594208/c-program-to-determine-levels-size-type-of-cache">another question</a>). As part of it, I was increasing the size of the array. And found that when I try to initialize an array: </p>
  
  <pre><code>int arr[2097152]; // 8MB
  </code></pre>
  
  <p>I got segmentation fault ... I think its because I am trying to declare an array that too large? Then I found the way to get around this is to use <code>malloc</code>. But being new to C (mainly use JavaScript/Python/Java ...). I get very confused with pointers and stuff ... </p>
  
  <p>I've declared an array 8MB: </p>
  
  <pre><code>int *arr = malloc (MBs * 1024 * 1024 / sizeof(int)); // MBs = 8
  </code></pre>
  
  <p>But now ... how do I access it, or write to it? When I use it like <code>arr</code> am I getting the address, and if I use <code>*arr</code> I get the 1st element? </p>