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 8 of 8 Topic 26085 of 26960
Post > Topic >>

Re: come in(I have a puzzled)

by Keith Thompson <kst-u@[EMAIL PROTECTED] > May 7, 2008 at 03:43 PM

Bart <bc@[EMAIL PROTECTED]
> writes:
> On May 7, 10:40 pm, Keith Thompson <ks...@[EMAIL PROTECTED]
> wrote:
>> Bart <b...@[EMAIL PROTECTED]
> writes:
>> > On May 6, 11:45 am, "Kevin" <yushuma...@[EMAIL PROTECTED]
> wrote:
>> >> Source:
>> >> #include <stdio.h>
>> >> #include <ctype.h>
>> >> void main()
>> >> {
>> >>     char a[]="this is the beautiful world!";
>> >>     char b[20];
>> >>     strcpy(b,a);
>> >>     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");
>>
>> >> }
>>
>> >> Now , question as follows:
>>
>> >> 1. Why "b" size unequal "a" size , but "b" can output "a" content?
>> >> 2. Why "if(strcmp(a,b)==0)" is true?
>>
>> > a and b are different types:
>>
>> Yes.
>>
>> > a is a pointer to a string
>>
>> No, a is an array of type char[29] (the length of the literal used to
>> initialize it plus 1 for the trailing '\0').
>
> Yes, of course, I read it as char *a (I think char a[] is pointer in
> some other context).

``char a[]'' as a function parameter declaration declares ``a'' as a
pointer (unfortunately, IMHO).

>> > b /is/ a string.
>>
>> No, b is an array of type char[20].  An array can *contain* a string.
>
> So some informality.. Here the intention is clearly a string.

The intention is that b is an array that contains a string (or rather,
it would contain a string after the strcpy() call *if* b were big
enough).  Before the strcpy() call, the contents of b are
indeterminate; it may or may not contain a string.

>> A "string" in C is a a data format, not a data type.
>>
>> > But a can obviously point to a string identical to what's in b.
>>
>> The object named ``a'' can't point to anything, since it's an array,
>> not a pointer.  However, the expression ``a'', in most but not all
>> contexts, has the value of a pointer to (the address of) the first
>> element of the object named ``a''.
>>
>> > 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).
>>
>> No, sizeof a == 20.
>
> Don't know about that, it seems a bit more. But different from sizeof
> b anyway, which was the original confusion.

Sorry, my mistake.  sizeof a == 29; sizeof b == 20.

>> > strcmp() compares two pointers to strings; but b is automatically
>> > converted to such a pointer, thanks to the way C handles arrays.
>
> Well if a and b are both arrays, then the reason why strcmp matches
> them but sizeof is different is a little different!
>
> (strcmp() only matches characters up to the nul terminator, ignoring
> any trailing characters in the array that still contribute to the
> sizeof property.)

Right.  But again, the program as originally posted invokes undefined
behavior.  One likely outcome is that, after the call to strcpy(), b
will contain the 20 characters "this is the beautifu" *without* a
trailing '\0'.  (There might happen to be a trailing '\0' after the
end of b -- or the strcpy might have clobbered something vital before
anything is printed.)

-- 
Keith Thompson (The_Other_Keith) <kst-u@[EMAIL PROTECTED]
>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
 




 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 Thu Jul 24 16:43:25 CDT 2008.