Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,strtok导致程序崩溃,即使在编译来自不同来源的示例代码时也是如此_C_Strtok - Fatal编程技术网

在C语言中,strtok导致程序崩溃,即使在编译来自不同来源的示例代码时也是如此

在C语言中,strtok导致程序崩溃,即使在编译来自不同来源的示例代码时也是如此,c,strtok,C,Strtok,使用下面演示的代码,程序崩溃。这是我复制粘贴的代码,根本没有修改它,但strtok似乎导致程序崩溃 #include <string.h> #include <stdio.h> int main() { char str[80] = "This is - www.tutorialspoint.com - website"; const char s[2] = "-"; char *token; /* get the first token */

使用下面演示的代码,程序崩溃。这是我复制粘贴的代码,根本没有修改它,但strtok似乎导致程序崩溃

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

int main()
{
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}
#包括
#包括
int main()
{
char str[80]=“这是-www.tutorialspoint.com-website”;
常量字符s[2]=“-”;
字符*令牌;
/*获取第一个令牌*/
令牌=strtok(str,s);
/*浏览其他代币*/
while(令牌!=NULL)
{
printf(“%s\n”,标记);
令牌=strtok(空,s);
}
返回(0);
}
我似乎找不到原因。我试图寻找strtok函数的源代码,但发现:

char*\uuu cdecl strtok(char*s1,const char*divident)
{
静态char*lastToken=NULL;/*不安全的共享状态*/
char*tmp;
/*如果是新字符串,则跳过前导分隔符*/
如果(s1==NULL){
s1=最后一个令牌;
如果(s1==NULL)/*故事结束*/
返回NULL;
}否则{
s1+=strspn(s1,定界);
}
/*查找段的结尾*/
tmp=strpbrk(s1,定界);
如果(tmp){
/*找到另一个分隔符,拆分字符串并保存状态*/

*tmp='\0';//->这似乎是出现故障的线路请查看。问题可能是本地环境和配置中的本地错误。您可以尝试使用其他计算机或按照链接进行操作,以确保获得预期的输出

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

int main()
{
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}
#包括
#包括
int main()
{
char str[80]=“这是-www.tutorialspoint.com-website”;
常量字符s[2]=“-”;
字符*令牌;
/*获取第一个令牌*/
令牌=strtok(str,s);
/*浏览其他代币*/
while(令牌!=NULL)
{
printf(“%s\n”,标记);
令牌=strtok(空,s);
}
返回(0);
}

如果您使用分析程序运行编译后的程序,例如,您会注意到代码中没有报告错误或错误,您可以调试崩溃,并在代码中程序崩溃的地方获得一条很好的错误消息。

您的代码在我看来是正确的。您的编译器或其associa安装中一定有错误ted库等。我尝试了这段代码,它运行正常。(除了前导分隔符,这是有问题的)第二个版本没有将多个连续分隔符视为一个(就像strtok()一样)。顺便说一句:strtok()是一个糟糕的函数。最好避免它。不要尝试对它进行emultate。问题可能是您正在修改字符串文字吗?str[0]=0也会崩溃吗?
#include <string.h>
#include <stdio.h>

int main()
{
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}