C 为什么这个代码在进入do循环之前给我一个segfault?

C 为什么这个代码在进入do循环之前给我一个segfault?,c,C,在进入do循环之前,有人能告诉我为什么这个代码会给我一个分段错误11吗 int parsePath(char *dirs[]){ char *pathEnvVar; char *thePath; for(int i=0; i<MAX_ARGS; i++) dirs[i]=NULL; pathEnvVar = (char *) getenv("PATH"); thePath = (char *) malloc(strlen(path

在进入do循环之前,有人能告诉我为什么这个代码会给我一个分段错误11吗

int parsePath(char *dirs[]){
    char *pathEnvVar;
    char *thePath;

    for(int i=0; i<MAX_ARGS; i++)
        dirs[i]=NULL;

    pathEnvVar = (char *) getenv("PATH");
    thePath = (char *) malloc(strlen(pathEnvVar) +1);
    strcpy(thePath, pathEnvVar);
    printf("the path is %s \n", thePath );
    /* Loop to parse thePath.  Look for a ':' delimiter between each path name */       
    const char delim[2] = ":";
    char *token;
    int counter = 0;
    /* get the first token */
    token = strtok(thePath, delim);
   printf("got to line 80 \n");
   printf("token is %s \n", token);
   printf("token is %u \n", (int)token);
   printf("got to line 83 \n");
   /* walk through other tokens */
   do 
   {
        printf("help me");
      counter++;
      strcpy(dirs[counter], token);
      printf("Path is %s", dirs[counter]);    

   }while((token = strtok(NULL, delim)));
   return 0;    
}

问题就在眼前

strcpy(dirs[counter], token);

开始时,将
dirs
中的所有元素初始化为
NULL
。因此,在上面的一行中,您将
标记
复制到
NULL

中,尝试添加一个新行来“帮助我”。我打赌它已经到了那一行了,但是由于缓冲的原因,它没有在屏幕上打印出来。谢谢。。。我从来没想过!现在,我可以精确地将SEG故障定位到strcpy线路
strcpy(dirs[counter], token);