title: Slicing in C compared with Python
body: <p>I have been trying to figure out a substring method like Python's splice, e.g. 'hello'[2:4].</p>
  
  <p><strong>OLD</strong>: The pointers <code>new</code> and <code>toret</code> (to return) are the same, but when stored in <code>hello</code>, it has a new address. Is there a way to maintain the address and get the slice printed out?</p>
  
  <p><strong>NEW</strong>: What is the best way to put chars in the buffer?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  //char * substr(char *str, int start, int end);
  //void substr(char *str, int start, int end);
  void get_substr(char *str, int start, int end, char *buffer);
  
  int main() {
      char word[] = "Clayton";
      char buffer[15];
      printf("buffer is now %s",get_substr(word,2,5,buffer);
      return 0;
  }
  
  
  void get_substr(char *str, int start, int end, char *buffer) {
      int length = end - start;
      printf("length %d\n",length);
      //char buffer[length]; //init normal array    
      //assign values from *str
      int i;
      for (i = 0; i &lt; length; i++) {
          buffer[i] = *(str+i+start);
      }
  }
  
  
  //char * substr(char *str, int start, int end) {
  /* char * substr(char *str, int start, int end) {
      int length = end - start;
      printf("length %d\n",length);
      char buffer[length]; //init normal array    
      //assign values from *str
      int i;
      for (i = 0; i &lt; length; i++) {
          buffer[i] = *(str+i+start);
          printf("i=%d buffer[i]=%c ptr=%c\n",i,buffer[i],*(str+i+start));
      }
      //add endline if not present
      if (buffer[length] != '\0') {
          buffer[length] = '\0';
          printf("buffer[%d] is %c\n",length,buffer[length]);
          printf("0 added\n");
      }
      printf("buffer %s %p\n",buffer,buffer);
      printf("len(buffer) is %d\n",strlen(buffer));
      char *toret;
      toret = (char*) &amp;buffer;
      printf("toret %s %p\n",toret,toret);
      return toret;
  } */
  </code></pre>
  
  <p>Sorry this may be a duplicate, but I can't find the same examples on this domain. <strong>I AM NEW TO C!</strong></p>