Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: come in(I h...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 8 Topic 26085 of 26821
Post > Topic >>

Re: come in(I have a puzzled)

by jt@[EMAIL PROTECTED] (Jens Thoms Toerring) May 6, 2008 at 11:11 AM

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
 




 8 Posts in Topic:
come in(I have a puzzled)
"Kevin" <yus  2008-05-06 18:45:41 
Re: come in(I have a puzzled)
Richard Heathfield <rj  2008-05-06 11:14:20 
Re: come in(I have a puzzled)
jt@[EMAIL PROTECTED] (Je  2008-05-06 11:11:41 
Re: come in(I have a puzzled)
Bart <bc@[EMAIL PROTEC  2008-05-07 14:18:17 
Re: come in(I have a puzzled)
ReplyDude <alss@[EMAIL  2008-05-07 15:30:47 
Re: come in(I have a puzzled)
Keith Thompson <kst-u@  2008-05-07 14:40:32 
Re: come in(I have a puzzled)
Bart <bc@[EMAIL PROTEC  2008-05-07 15:07:59 
Re: come in(I have a puzzled)
Keith Thompson <kst-u@  2008-05-07 15:43:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:46:42 CDT 2008.