title: Creating Variable Sized char Array in C
body: <p>I am trying to take in a char array that is oversized in order to place delimiters in it based on the variable <code>maxSize</code></p>
  
  <p>For example if the string <code>"This is a message"</code> is passed to the function along with a <code>maxSize</code> of <code>4</code> then the output should be <code>"This, is ,a me,ssag,e"</code> </p>
  
  <pre><code>char *placeDelimiter(char message[], int maxSize) {
    int msgSize = strlen(message);
    int delSize = (msgSize/maxSize);
    int remSize = msgSize%maxSize;
    int newSize = msgSize+delSize;
    if (remSize==0) delSize--; //removes delimiter if on end of char array
    char temp[newSize];
    int delPos = 0;
    for (int x=0;x&lt;msgSize;x++) {
      if ((x+1)%maxSize == 0) temp[x] = ',';
      temp[x+delPos] = message[x];
      delPos = (x+1)/maxSize;
    } 
    return (char *)temp;
  }
  
  int main()
  {
      char msg[] = "This is a message";
      char *p;
      p = placeDelimiter(msg, 4);
      printf("%s", p);
      return 0;
  }
  </code></pre>
  
  <p>My problem is that I am getting the output <code>"This i,"</code> from the input <code>"This is a message"</code> (From an online compiler). Can anyone explain to me what I am doing wrong and how to fix it?</p>