title: Memory allocation using for loop
body: <p>My Doubt is regarding only memory allocation so don't think about program output</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int main(){
  
    for(int i=0;i&lt;20;i++){
       char *str=malloc(sizeof(char)*6); //assuming length of each string is 6
       scanf("%s",str);
       insertinlinkedlist(str);
    } 
  
  } 
  </code></pre>
  
  <p>whenever i allocate memory here as shown above only the base address of char array will pass to linked list,and that is the memory block allocated for char  array is inside main only and i am storing the base address of that array in <code>str</code> which is local to main and is passed to <code>insetinlinkedlist</code>   </p>
  
  <p>I want to ask whenever memory is allocated inside loop than why the number of 
  memory blocks(no of char arrays declared ) are created equal to <code>n</code> (number of time loop runs) since variable name is same we should be directed to same memory location
  Note I have checked in compiler by running the loop all the times when loop runs memory the value of <code>str</code> is different</p>
  
  <blockquote>
    <p>is The above method is correct of allocating memory through loop and through same variable "Is the method ensures that every time we allocate memory in above manner their will be no conflicts while memory allocation and every time we will get the address of unique memory block"</p>
  </blockquote>
  
  <p>Now above doubt also creates a doubt in my mind
  That if we do something like that </p>
  
  <pre><code>int main(){
     for(int i=0;i&lt;n;i++){
     array[50];
  
  
     } 
  }
  </code></pre>
  
  <p>then it will also create 50 array inside stack frame  </p>