Thread: Problem Returning Value

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    Problem Returning Value

    Hi guys, I've been trying to get this function to work but nothing seems to be doing it. I'm basically comparing two strings: if they have no identical letters in them, I return true, if they have one or more letters common to both strings I return false. It works for the true cases and seems to work for the false cases. The problem is, instead of returning false it returns a random number such as 41256. Can anyone point out where the problem is?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    bool i........eroPair (char *a,char *b)
        {
            int i,j;
            for (i=0;i<strlen(a);i++)
        {   for (j=0;j<strlen(b);j++)
            {if(*(a+i)==*(b+j))
            return false;
            else
            ;
            }
        }
           return true;
        }
    
    
    
    int main(void)
    
    {
     char *string1="ABCD";
     char *string2="EFGH";
     i........eroPair(string1,string2);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He said "doing it".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Nice

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Also

    "Array names are not pointers, so you cannot increment them.


    Quzah. "

    Lol.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorry, I couldn't help myself.

    As to your second post, it's true. I'm not sure what relevance that has here, but you can't increment a pointer name. You are apparently taking this out of the context of a thread comparing pointers to arrays. Anyway, I'm right.

    Edit. Nevermind, you're basically doing a strchr loop.
    Code:
    bool foo( char *s1, char *s2 )
    {
        while( s1 && *s1 )
        {
            char *p = s2;
            for( p = s2; *p; p++ )
                if( *s1 == *p ) return false;
            s1++;
        }
        return true;
    }
    Your example never actually does anything with your return value though.

    Quzah.
    Last edited by quzah; 11-20-2010 at 11:05 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Okay, I see what you're getting at but I don't understand why it isn't returning false and instead is returning '4206592'. I also tried making a function to determine if a string is symmetric, it also does the same thing (works properly for true cases, returns '4206592' for false cases).

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Your function also works but still it returns that 420...number.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a small example to test it:
    Code:
    #include<stdio.h>
    #include<stdbool.h>
    bool retfalse( void ) { return false; }
    int main( void )
    {
        printf( "false is %d, and retfalse() gives %d\n", (int) false, (int) retfalse() );
        return 0;
    }
    Your example isn't actually doing anything with a return value, so I can't tell why you're getting the value you aren't expecting.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Okay, I tried using the result in a simple if statement in the main function and it works properly. I guess it doesn't matter why it was returning 420.....

    Problem averted!

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could make your life easier by having it return 0 for false, 1 for true.

    The bizarre number, which looks like a pointer address, will work as long as your are comparing false to false but it's not going to work with the ! operator and may cause other logic problems in your code.

    You should open the stdbool.h header and have a look... This is the standard one...

    Code:
    #ifndef _STDBOOL_H
    #define _STDBOOL_H
    
    /* stdbool.h - C99 standard header */
    
    /* macros */
    #define bool  _Bool
    #define false  0
    #define true  1
    #define __bool_true_false_are_defined  1
    
    #endif /* _STDBOOL_H */

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Funny enough, returning 0 also returns that 420...value. By this I mean i run the original code I posted and the run screen will show 'Process returned 4206592'.

    When I call the function inside an if statement in the main program it works properly. i.e. if the function returns false, it goes to my else statement if not it does the if portion. Weird.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Quote Originally Posted by CProgramming11
    Funny enough, returning 0 also returns that 420...value. By this I mean i run the original code I posted and the run screen will show 'Process returned 4206592'.
    I think that you are confusing the return value of the function with the exit status of the program (process).

    I suggest that you compile and run this program, which is a cleaned up version of what you posted originally:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    bool foo(const char *a, const char *b)
    {
        size_t i, j, len_a = strlen(a), len_b = strlen(b);
        for (i = 0; i < len_a; i++)
        {
            for (j = 0; j < len_b; j++)
            {
                if (a[i] == b[j])
                    return false;
            }
        }
        return true;
    }
    
    int main(void)
    {
        const char *string1 = "ABCD";
        const char *string2 = "DEFGH";
        printf("%d\n", foo(string1, string2));
        return 0;
    }
    Is the output 0? If so, there is no problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Yes that program works fine.

    You're right, I just confused the return value for the exit status. I'm used to seeing the return value as the exit status of the program. Newb mistake. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  2. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 04:26 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM