come in(I have a puzzled)

Discussion in 'C Programming' started by Kevin, May 6, 2008.

  1. Kevin

    Kevin Guest

    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?
    Kevin, May 6, 2008
    #1
    1. Advertising

  2. Kevin <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 6, 2008
    #2
    1. Advertising

  3. Kevin

    Bart Guest

    On May 6, 11:45 am, "Kevin" <> 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:

    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
    Bart, May 7, 2008
    #3
  4. Kevin

    ReplyDude Guest

    replied!

    Bart Wrote:

    > On May 6, 11:45 am, "Kevin" <> 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:
    >
    > 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
    ReplyDude, May 7, 2008
    #4
  5. Bart <> writes:
    > On May 6, 11:45 am, "Kevin" <> 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').

    > b /is/ a string.


    No, b is an array of type char[20]. An array can *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.

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


    I think the other errors in the program have already been pointed out,
    but ...

    Drop the ``#include <ctype.h>''; it's not needed.
    Add ``#include <string.h>''; it *is* needed.
    Change ``void main()'' to ``int main(void)''.
    Change ``char b[20];'' to, for example, ``char b[30];''.
    In the printf call, change ``sizeof(b)'' to ``(int)sizeof b'';
    the "%d" format requires an int, not a size_t.
    Before the closing brace, add ``return 0;''.
    Enable warnings in your compiler, and buy some whitespace (it's really
    cheap).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 7, 2008
    #5
  6. Kevin

    Bart Guest

    On May 7, 10:40 pm, Keith Thompson <> wrote:
    > Bart <> writes:
    > > On May 6, 11:45 am, "Kevin" <> 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).

    > > 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.


    >
    > 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.

    >
    > > 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.)

    --
    Bartc
    Bart, May 7, 2008
    #6
  7. Bart <> writes:
    > On May 7, 10:40 pm, Keith Thompson <> wrote:
    >> Bart <> writes:
    >> > On May 6, 11:45 am, "Kevin" <> 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 7, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Andrew

    VHDL: puzzled beginner

    Mark Andrew, Jun 27, 2004, in forum: VHDL
    Replies:
    5
    Views:
    631
    Mark Andrew
    Jun 30, 2004
  2. Daniel

    still puzzled

    Daniel, Jun 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    642
    Steven Cheng[MSFT]
    Jun 9, 2004
  3. schiefaw

    Very Puzzled, Please Help

    schiefaw, Jul 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    370
    schiefaw
    Jul 22, 2005
  4. Replies:
    5
    Views:
    1,390
  5. shoplifes
    Replies:
    0
    Views:
    303
    shoplifes
    Nov 25, 2007
Loading...

Share This Page