为什么要为此数组中的第一个元素打印垃圾?

use*_*777 0 c string

  #include <stdio.h>
  #include<string.h>
  #include<stdlib.h>

  int main()
  {
     char *words[] = {"mHello", "kWorld", "kHow", "9Are", "3You?"};
     char **parsed = malloc(5);
     int i;
     for (i = 0; i < 5; i++)
     {
        int n = strlen(words[i]);
        parsed[i] = malloc(n);
        strncpy(parsed[i], words[i] + 1, n);
        printf("[%s] ", parsed[i]); 
     }
     printf("\n----------------------\n");
     for (i = 0; i < 5; i++)
       printf("[%s] ", parsed[i]);
         return 0;
  }
Run Code Online (Sandbox Code Playgroud)

parsed[i]包含words[i]没有第一个字符.

输出是

 [Hello] [World] [How] [Are] [You?]
 ----------------------
 [?? o] [World] [How] [Are] [You?]
Run Code Online (Sandbox Code Playgroud)

为什么第一个printf调用parsed[0]正常工作而第二个调用没有?

此外,如果我从中删除一个元素words,此代码工作正常.到底是怎么回事 ?

Jac*_*ack 9

malloc没有为指向字符串的指针分配正确的空间,它应该是

parsed = malloc(sizeof(char*)*5)
Run Code Online (Sandbox Code Playgroud)