Kevin <yushumao65@[EMAIL PROTECTED]
> wrote:
> Source:
> #include <stdio.h>
> #include <ctype.h>
That's not needed but you're missing
#include <string.h>
> void main()
main() always returns an it, so make that
int main( void )
> {
> char a[]="this is the beautiful world!";
> char b[20];
> strcpy(b,a);
Pfff. You just wrote past the end of array 'b'. All bets are off.
> printf("char array b size is(%d),The content of b
is:%s\n",sizeof(b),b);
> if(strcmp(a,b)==0)
> printf("a equal to b!\n");
You're missing a
return 0;
> }
> Now , question as follows:
> 1. Why "b" size unequal "a" size , but "b" can output "a" content?
Because you invoked undefined behaviour by writing past the end of b.
Everything can happen from now on, the program may even look as if it
would be working correctly.
> 2. Why "if(strcmp(a,b)==0)" is true?
Because you invoked undefined behavior and the way memory is
set aside for both the arrays by your compiler happened to
make that result possible.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@[EMAIL PROTECTED]
\__________________________ http://toerring.de


|