Home > other >  GCC compiler, there was a death execution cycle, a result is output, a great god help and see what h
GCC compiler, there was a death execution cycle, a result is output, a great god help and see what h

Time:10-13

Here comes with the original code: the background is using GCC coding under Linux system, there was a dead loop calling function, to understand where death, added a print num statements, personal think calling function statements will be executed twice, until meet 3, program into the infinite loop; After the final program can output two results into an infinite loop, but the results are compiled result is directly into the infinite loop, without any results output,

#include

Int str_fun (char * p);
Int main (int arg c, char * argv [])
{
Char [] s="ho3llewrftridgeefvhtydv";
int n;
N=str_fun (s);

Printf (" n=% d \ n ", n);

return 0;
}
Int str_fun (char * p)
{
Int num=0;
While (* p!='\ 0') {
If (* p<='z' & amp; & * p> {
='a')Num++;
Printf (" % d ", num);

P++;
}}
return num;

}

CodePudding user response:

Change the string to global production

CodePudding user response:

I have this program with a pointer to the string, this does not affect, if I put the string all letters, not Numbers, the program can normal execution,
The question now is I already know why my program dead cycle, when the * p=3, the program will not enter the if statement, p++ not executed, thus appeared the infinite loop, this I know, but the first two cycle carried out as normal, the individual feels output will be two times the value of num, but actual of infinite loop directly, without any output,

CodePudding user response:

You met this program is beyond the scope of a to z characters of dead cycle, p++ if judgment outside

CodePudding user response:

If you add a sleep after each printing can be printed
  • Related