C 而循环的行为不符合预期

C 而循环的行为不符合预期,c,linux,ubuntu,netbeans,C,Linux,Ubuntu,Netbeans,我在一项任务中工作,遇到了一些奇怪的事情。我的程序中有一个while循环,它似乎没有分支到for循环。我放置了两个打印语句,只有“1”反复打印。请注意,只有在从linux终端编译和运行时才会发生这种情况。现在看起来很奇怪的是,如果我在Netbeans中运行完全相同的代码(while loop加上其他所有代码),它的编译和行为似乎与预期的一样。任何人都知道可能出了什么问题。这是代码。我感谢你的帮助 while(strstr(p,string_a)!= NULL) { p = trailer

我在一项任务中工作,遇到了一些奇怪的事情。我的程序中有一个while循环,它似乎没有分支到for循环。我放置了两个打印语句,只有“1”反复打印。请注意,只有在从linux终端编译和运行时才会发生这种情况。现在看起来很奇怪的是,如果我在Netbeans中运行完全相同的代码(while loop加上其他所有代码),它的编译和行为似乎与预期的一样。任何人都知道可能出了什么问题。这是代码。我感谢你的帮助

while(strstr(p,string_a)!= NULL)
{
    p = trailerp + pholderp; 
    long int index =  strstr(p,string_a) - (p+1); // -1 where it hits 
    printf("1");
    for(  i = 0; i <= index; i++)
    {
        printf("2");
        p2[trailerp2] = pholderp[trailerp];
        trailerp++;
        trailerp2++;
        if(i == index)
        {  
            int j;
            for(j=0; j <= lenb-1; j++)            // insert the new string
            {
                p2[trailerp2] = string_b[j];
                trailerp2++;
            }
            trailerp++;
        }       
    }  
}    
while(strstrstr(p,string_a)!=NULL)
{
p=牵引器p+pholderp;
long int index=strstrstr(p,string_a)-(p+1);/-1
printf(“1”);

对于(i=0;i这是因为
strstr(p,string_a)
在此部分返回
p
0

long int index =  strstr(p,string_a) - (p+1); // -1 where it hits 
这将导致
索引<0
,并防止进入循环


您必须在此语句之前立即打印
p
string\u a
,以查看出现了什么问题。

如果您修复了格式,可能会有所帮助。这听起来好像您没有正确初始化变量。您真的应该学会使用警告和调试信息进行编译(例如,将
-Wall-g
传递给
gcc
编译器),您应该学会使用
gdb
调试器。