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 4 of 8 Topic 26067 of 27343
Post > Topic >>

Re: come in(I have a puzzled)

by Bart <bc@[EMAIL PROTECTED] > May 7, 2008 at 02:18 PM

On May 6, 11:45=A0am, "Kevin" <yushuma...@[EMAIL PROTECTED]
> wrote:
> Source:
> #include <stdio.h>
> #include <ctype.h>
> void main()
> {
> =A0 =A0 char a[]=3D"this is the beautiful world!";
> =A0 =A0 char b[20];
> =A0 =A0 strcpy(b,a);
> =A0 =A0 printf("char array b size is(%d),The content of b
is:%s\n",sizeof(=
b),b);
> =A0 =A0 if(strcmp(a,b)=3D=3D0)
> =A0 =A0 =A0 =A0printf("a equal to b!\n");
>
> }
>
> Now , question as follows:
>
> 1. Why "b" size unequal "a" size , but "b" can output "a" content?
> 2. Why "if(strcmp(a,b)=3D=3D0)" is true?

a and b are different types:

a is a pointer to a string
b /is/ a string.

But a can obviously point to a string identical to what's in b.

The size of a will be 4 or whatever, the size of b will be 20
(although it should be more in this case otherwise your "this is..."
string will overflow it).

strcmp() compares two pointers to strings; but b is automatically
converted to such a pointer, thanks to the way C handles arrays.

--
Bartc
 




 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 Sat Sep 6 21:39:25 CDT 2008.