title: C: Core dumped with realloc()
body: <p>Before starting, I should say I have seen several posts around this topic (like <a href="https://stackoverflow.com/questions/32113922/getting-core-dumped-when-using-realloc">this one</a>, but I am still missing something.  I am quite new to C, so please bear with me.  I am attempting to construct a function that copies a string from one pointer location to another.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;string.h&gt;
  
  void astrncpy(char **base, char *copyme){
  
      printf("Copying '%s' into a location currently featuring the following string: '%s'\n", copyme,*base);
      printf("The location of our string to be copied (%s) is %d.\n", copyme, &amp;copyme);
      printf("The location of our string to be replaced (%s) is %d.\n", *base, base);
  
      //Declare string length variable
      int new_len=strlen(copyme);
      printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
  
      //Reallocate pointer array
      *base=realloc(*base,sizeof(char)*new_len+1);
      printf("Reallocating memory allocation.\n");
  
      //Copy copyme content to base string location
      strncpy(*base,copyme,new_len);
  
      printf("The string at location %d is now %s\n", base, *base);
  
  }
  
  void main(){
  
      //Declare iterator
      int i;
  
      //Generate strings
      char first_lit[]="Fortran?";
      char second[]="Now that's a name I've not heard in a long time.";
  
      //Convert first string to array (so we can get at the pointer to the strings pointer)
      char **first; //Declare pointer to pointer array that represents the first string
      first=malloc(strlen(first_lit)*sizeof(char)); //Allocate space for the pointer array
      *first=first_lit; //Assign values to the pointer locations
  
      //Copy copyme into base
      astrncpy(first,second);
  }
  </code></pre>
  
  <p>When I attempt to reallocate within <code>astrncpy()</code>, the core dumps.  From what I understand, this occurs if the pointer array is either not NULL, or is not the product of <code>malloc()</code>.  I feel like I am not failing that test.  Any guidance on what is happening would be appreciated.</p>
  
  <p>Bonus points for insight on my construction of the input <code>*base</code>.  I spent a good deal of time experimenting to determine what an acceptable input would be for the <code>**base</code> argument (note that both arguments to <code>astrncpy()</code> are given in the text I am working through).  I am not clear, however, on why I can't get there with <code>first_lit</code> instead of having to construct <code>first</code>.  The string is still an array, is it not?  Again, help is appreciated.</p>
  
  <p><strong>UPDATE</strong>:  I put this down for a while to do work I actually get paid to do, but coming back to it, I couldn't shake the idea that the declaration modifications in the response below weren't necessary.  (This is because they had not yet been covered in the text.)  In any event, I will still give a check to the solution because it works and was helpful on multiple fronts.  It should be noted, however, that the following also works:</p>
  
  <pre><code>/*Program demonstrates string manipulation operations*/
  
  #include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;string.h&gt;
  
  void astrncpy(char **base, char *copyme){
  
      printf("\nWe are inside the function, astrncpy.\n");
      printf("\nCopying '%s' (*copyme) into a location currently featuring the following string: '%s' (**base)\n", copyme,*base);
      printf("The location of our string to be copied (%s) is %p.\n", copyme, &amp;copyme);
      printf("The location of our string to be replaced (%s) is %p.\n", *base, base);
  
      //Declare string length variable
      size_t new_len=strlen(copyme);
      printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
  
      //Reallocate pointer array
      printf("Reallocating memory block associated with base string to be replaced.\n");
      *base=realloc(*base,sizeof(char)*(new_len+1));
  
  
      //Copy copyme content to base string location
      strncpy(*base,copyme,new_len);
  
      printf("The string at location %p is now %s\n", base, *base);
  
  }
  
  void main(){
  
      //Declare iterator
      int i;
  
      //Generate strings
      char first_lit[]="Fortran?";
      char second[]="Now that's a name I've not heard in a long time.";
      //int testint=5;
  
      //Capture elements of first_lit in pointer array (so we can get at the pointer to the strings pointer)
      char *first; //Declare pointer to pointer array that represents the first string
      first=malloc((strlen(first_lit)+1)*sizeof(char)); //Allocate space for the pointer array
      strncpy(first,first_lit,strlen(first_lit)); //Assign values to the pointer locations
  
      //Copy copyme into base
      printf("Initiating copy operation...\n");
      astrncpy(&amp;first,second);
  }
  </code></pre>