Thread: functions and if question please help

  1. #1
    Unregistered
    Guest

    functions and if question please help

    If(strcmp(name,”james”)==0)

    I would like to know why the == to 0 in this statement please help.

    &&

    void function ( char name [ ] );

    Why is it not necessary to give a array size in the function

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Well buddy,
    first question's answer:

    strcmp function will return an integer 0 or -1 or +1 depending on the result of the comparision of the two strings. If the two strings are identical a zero is returned. Now, 'C' is stubborn and its rule says if you should compare two values you should use ==. If you use = instead of == then you would be assigning that value as the answer and no matter what those two strings are you would always land up in the true block of the if.
    hope you got my point.
    Regards
    Help everyone you can

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    It looks like you have a function prototype there.

    When prototyping a function it is only necasary to tell the compiler that you wish to pass an array of type char, (watch the compiler complain if later you pass an array of type int )

    You do not specify the size at this stage as this is done when you actually call the function and pass the array.

    i.e.

    /* Prototype */
    void function ( char name [ ] );

    /* Function */
    void function (char name [INDEX])
    {
    Do stuff here;
    }
    Last edited by foniks munkee; 02-15-2002 at 09:33 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why is it not necessary to give a array size in the function
    Because it doesn't matter, array notation degrades into pointer notation anyway so if you include the array size it will just be discarded. Some people prefer to add it for clarity when reading the code, but the compiler sees both
    void function ( char name [ ] ); and
    void function ( char name [SIZE] );
    as
    void function ( char *name );

    Ever wonder why you pass an array to a function in the actual call like this?
    function ( array );
    It's because you pass the function a pointer to the first element in the array, not the entire array.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed