Function and pointer problems

This is a discussion on Function and pointer problems within the C Programming forums, part of the General Programming Boards category; This is a nice little assignment my prof. decided to spring on us out of nowhere, (thanks prof. ). But ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Function and pointer problems

    This is a nice little assignment my prof. decided to spring on us out of nowhere, (thanks prof. ). But anyways what I need to do is
    write a function that will search a string for any one of a given set of characters.

    The idea is to locate the first character in the source string that matches any of the characters in the chars string. The function then returns a pointer to the place in source where the first match was found.

    If none of the characters in source match any of the chars, then a NULL pointer is returned. If either argument in NULL, or either string is empty, then a NULL pointer is returned.

    But I'm not allowed to use any of the library string routines (strcpy, strcmp, strlen, index, etc.) just pointers to manipulate them to get
    the desired result. I'm still kind of new to both pointers, and functions, but I"m to figure simple and regular ones, like the ones shown in the tutoriols
    on the site. But I can seem to figure out more complex ones like this one.


    Code:
    #include <stdio.h>
    
    
    //The function Prototype that I have to use
    char *find_char( char const *source, char const *chars );
    
    
    
    void 
    char *find_char( char const *source, char const *chars )
    {
         char  i; // using this to go through each string and look for the first
                  // match
         
        for( i = source; i <= source; i++ )
             for( i = chars; i <= chars; i++ )
             
             if (i(source) == i(chars) // try to compare the strings not sure if the 
                                                // syntax is correct
             return &i(source);
             
        else
            return NULL;
             
               
    }
    
    void main()
    {
        char myString = abcdef;
        char nyString = ttyabn;
    
    
        
        char j;
        
        printf("The original strings %s\n", myString, nyString);
        *find_char(&myString, &nyString);
        printf("The first matching character is %s\n", j);
    }
    These are the errors I keep getting:



    just-test.c:63: error: two or more data types in declaration of `find_char'
    just-test.c: In function `find_char':
    just-test.c:66: warning: assignment makes integer from pointer without a cast
    just-test.c:66: warning: comparison between pointer and integer
    just-test.c:67: warning: assignment makes integer from pointer without a cast
    just-test.c:67: warning: comparison between pointer and integer
    just-test.c:69: error: called object is not a function
    just-test.c:69: error: called object is not a function
    just-test.c:71: error: syntax error before "return"

    just-test.c: In function `main':
    just-test.c:81: error: `abcdef' undeclared (first use in this function)
    just-test.c:81: error: (Each undeclared identifier is reported only once
    just-test.c:81: error: for each function it appears in.)
    just-test.c:82: error: `ttyabn' undeclared (first use in this function)

    just-test.c:80: warning: return type of 'main' is not `int'

    make.exe: *** [just-test.o] Error 1

    Execution terminated


    I'm still trying to figure this out now but any help anybody can give me will be well apprieciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If abcdefg is supposed to be a string literal, then try enclosing it in a pair of quotation marks. Also, 'myString', isn't. It's a single character. You meant to have a pointer there, or an array. You have a 'void' before 'char *' in your function definition. Remove it.

    You know, if you actually read the errors and then your code, you'll get a lot further.


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

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well you really should listen to what your compiler is telling you. First up, main returns an int, so declaring it as void is wrong. When you call your function, find_char, it should not look like you had it:

    Code:
    *find_char(&myString, &nyString);
    This would work:

    Code:
    find_char(&myString[0], &nyString[0]);
    Though it is not as much idiom as this:

    Code:
    find_char(myString, nyString);
    Ok, so with your function call straightened out, on to the function:

    Code:
    for( i = source; i <= source; i++ )
    You assign 'source' to i - which happens to be an address. Of course this does not really work for you, because i is not actually a pointer variable, but a char. Then you test if i is less than or equal to source, which it is, because you just assigned source to i. Even if i was a pointer variable, this would not make a lot of sense, as you just assigned source to i, so of course they will be equal. And then the loop ends. So you need to rework that somehow.

    You should try working out on paper the steps you need to take before you actually start trying to implement it in real code.

    For the function proper, it might look something like this:

    Code:
    check if either source or chars are NULL
        if either are NULL, return NULL
    After you have checked the pointers for NULL (you don't want to try dereferencing a NULL pointer) you can go ahead and see if either of the strings are empty:
    Code:
    check if either source or chars are empty
        if either are empty, return NULL
    Then we can get on with comparing the characters in 'chars' to the characters in 'source':
    Code:
    while there is still a character in 'source'
          while there is still a character in 'chars'
                 does the current char in 'source' match the current char in 'chars'?
                                if yes, return the address of 'source'
                                if no, then increment to the next character of 'chars' and check again
          none of 'chars' match the current character in 'source'?
                  increment to next character in 'source'
                  reset to beginning of 'chars' and start looking again
          no match found?
                  return NULL
    That should give you some ideas to think about. Also, when you want to compare actual characters, you need to dereference. So comparing 'source' and 'chars' you would do something like this:

    Code:
    if (*chars_pointer != '\0'
                                && *chars_pointer == *source) {
                                    return source;
    Without the '*' you are comparing addresses, and not the actual value stored there. Finally, once you get this thing working, you might be interested in experimenting with different ways of improving your alogrithm. The 'pseudo-code' I demonstrated is not the most efficient algorithm - there are more efficent ways of doing it, but different ways can be explored once you get this one figured out.

    ~/
    Last edited by kermit; 11-13-2005 at 01:28 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Thanks guys for your help so far, I've got a better understanding of this but I still have a problem. I'm trying figure out how I can use char i to go througth the two strings and look for the first matching charactor. Right now for the first matching charactor I'm getting null.





    Code:
    char *find_char( char const *source, char const *chars )
    {
         char  i; // using this to go through each string and look for the           
                    // first match
                  
         char sourc;
         char charr;
         
         source = &sourc;
         chars = &charr;
       
        if (*source == NULL || *chars == NULL)
           return NULL;
        
        if (sourc == NULL || chars == charr)
           return NULL;
    
         
       
        
     while (*source != NULL && *chars != NULL)
              
              if (*chars != '\0'
                                && *chars == *source) 
                                    return *source;
    
              else
                  chars ++;
              
             
               
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I would think

    Code:
    char* find_char(const char *src, const char *chrs)
    {
    int  i;
    
    
    
    if ( ! src || !*src  || ! chrs || !*chrs )
       return(NULL);
    
    while ( *src )
         {
          for ( i = 0 ; chrs[i] ; ++i )
              if ( *src == chrs[i] )
                  return( (char*)src);
          ++src;
          }
    
    return(NULL);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21