Home > Back-end >  In c language using the scanf addresses to a character variable assignment, why can correct output b
In c language using the scanf addresses to a character variable assignment, why can correct output b

Time:10-08

In c language using the scanf addresses to a character variable assignment, why can correct output but is unable to correctly use STRCMP function

CodePudding user response:

String manipulation requirements end 0
There is no 0
wrong

CodePudding user response:

reference 1st floor lin5161678 response:
string operation requirements end 0
There is no 0
Wrong

This application is correct, but why can't use STRCMP substitution function

CodePudding user response:

reference 1st floor lin5161678 response:
string operation requirements end 0
There is no 0
Wrong

# include & lt; String. H>
# include & lt; Stdio. H>
Int main ()
{
Int I, findFlag=0;
Char x;
Char STR [] [13]={" Pascal ", "Basic", "Fortran," "Java", "Visual C", "Visual Basic"};
FindFlag=0;
Printf (" please input a string: ");
The scanf (" % s ", & amp; X);

i=0;
While (i<6 & amp; & ! FindFlag)
{
If (STRCMP (& amp; X, STR [I])==0)
{
FindFlag=1;

}
i++;
}

If (findFlag)
{
Puts (& amp; X);
}
The else
{
Printf (" not found! \n");
}
return 0;
}

CodePudding user response:

Vc + + 6.0, one of my favourite, no
 # include & lt; String. H> 
# include & lt; Stdio. H>

Int main (int arg c, char * argv [])
{
Int I, findFlag=0;
Char [13] x={0};//-- -- -- -- -- -- -- -- -- into a character array, or string -- -- -- -- -- -- -- --
Char STR [] [13]={" Pascal ", "Basic", "Fortran," "Java", "Visual C", "Visual Basic"};
FindFlag=0;
Printf (" please input a string: ");
The scanf (" % s ", & amp; X);

i=0;
While (i<6 & amp; & ! FindFlag)
{
If (STRCMP (x, STR [I])==0)//-- -- -- -- -- -- -- to x, the string first address -- -- -- -- -- -- -- -- -- -- --
{
FindFlag=1;

}
i++;
}

If (findFlag)
{
Puts (x);//-- -- -- -- -- -- -- to x, the string first address -- -- -- -- -- -- -- -- -- -- --
}
The else
{
Printf (" not found! \n");
}
return 0;
}

Debugging results

CodePudding user response:

The
119 reference 3 floor??????? 110 response:
Quote: refer to 1st floor lin5161678 response:
string operation requirements end 0
There is no 0
Wrong

# include & lt; String. H>
# include & lt; Stdio. H>
Int main ()
{
Char x;
The scanf (" % s ", & amp; X);

Success is an array GG input

CodePudding user response:

Since it can output string, why can't directly use STRCMP function, and to define an array, I send some of the output of the program is right, or wrong, don't know the reason

CodePudding user response:

This program belongs to the dangerous, the input string if more than the length of the memory of x causes memory of crossing the line, for example, of course, program, memory cross-border impact, so can work correctly, but if plus STRCMP behind the function call, the function call stack memory address information is destroyed, cause the function call to return less than the correct memory address, so make a mistake,

CodePudding user response:

refer to 7th floor qybao response:
this program belongs to the dangerous, if more than x string memory length can cause memory of crossing the line, for example program, of course, crossing the line cause no memory, so can work correctly, but if plus STRCMP behind the function call, the function call stack memory address information is destroyed, cause the function call to return less than the correct memory address, so make a mistake,

That if there is no more than its storage area that is feasible

CodePudding user response:

No more than is possible, but as long as you input a character will be super, because string and an ending character '\ 0'
About the memory of crossing the line, you can Google baidu function information and in the process of the function call stack management, so you know memory cross-border stack to what position can destroy what kind of information,

CodePudding user response:

references 9 f qybao response:
no more than is possible, but as long as you input a character will be super, because the string has a end '\ 0'
About this memory of crossing the line, you can Google, baidu, the function of information and in the process of the function call stack management, so you know memory cross-border stack to what position can destroy what kind of information,

Good dalai thank
  • Related