strcmp 函数库中的声明:
int __cdecl strcmp (
const char * src,
const char * dst
)
#include<string.h>
#include<stdio.h>
int main( void )
{
char *s1 = "abcdkkkd";
char *s2 = "oefjeofjefo";
printf( "%d", strcmp( &s1, &s2 ) );
return 0;
}
程序为什么会正常运行?
int strcmp( const char *lhs, const char *rhs );
Function declaration, c99 is slightly different from c11, but it does not affect it.
What is passed in is a pointer. Even if a string variable is passed in, it is still an address.
compares the strings starting from the pointer address to the null character
I tried it on vs2015 and got an error: "The actual parameter of type char* is incompatible with the formal parameter of type const char"
I failed to compile. Tip
error: cannot convert 'const char**' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
. But you, the poster, the parameter passed in is not the address of that string! What you are passing in is the address of the variable.s1
and&s1
are different!The Gcc compilation result is as follows:
strcmp.c:9:5: warning: passing argument 1 of 'strcmp' from incompatible pointer type [enabled by default]
You only passed the compilation because it was passed in All are addresses, so only a warning is reported, but the actual running result is still wrong.