Thread: Search with pointers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Search with pointers

    Hi ,I am learning C and I am stack with this code ,any help will be greatly appreciated!

    Code:
    #include <stdio.h>
    char * findChar(char a[],int size,char key);
    int main()
     {       
        char *keyValue,key;
        int size=20;
        char a [21];               
        printf("Enter the key: ");
        scanf("%c",& key);
        printf("Enter an input up to 20 characters: ");
        scanf("%s",a);
        keyValue=findChar(a,size,key);
        printf("%c",keyValue);
        getch();
        return 0;
     }
     
      char * findChar(char a[],int size,char key)
       {
         int i;
         char *pfounded;
           for(pfounded=a,i=0;i<size;i++)
           {               
              if(*(pfounded+i)==key)     
               {
                 return *(pfounded+i);
               }
             else
                return NULL;
            }
        }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    We can't really help you unless you tell us what exactly about this code you're stuck with. Asking specific questions will generally get you better, more relevant answers.

  3. #3
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    when i run this my compiler gives me three errors..

    (13): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'char *'.
    (14): warning #2027: Missing prototype for 'getch'.
    (26): error #2060: Illegal return type; expected 'char *' but found 'char'.


    the 13, 14 and 26 are your line-numbers... on which the error is presented
    the rest of the error is what's actually wrong... if you change that it could possibly work..
    Because Tetris Is Unrealistic.

    "The fear of death is the most unjustified of all fears, for there's no risk of accident for someone who's dead." - Albert Einstein

    "The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over." - Hunter S. Thompson

    "I never think of the future. It comes soon enough." - Albert Einstein

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    The function supposed to return a pointer to the first occurrence of the character key in the array,and takes the array ,the size of the array and the search key

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The else return NULL, is up too high. Move it outside the for loop's closing brace.

    The first time it isn't the key, the else kicks in, and the function returns. Also, NULL isn't a char *. Assign your pointer to NULL, and return it. Test for NULL, in the code immediately after the return.

    If you remove the asterisk *, the return looks okay, for when you do find the key. As noted above, you're returning a char, not a char *.
    Last edited by Adak; 11-04-2010 at 05:50 AM.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    with pointer
    Code:
      char * findChar(char a[],int size,char key)
       {
         char *end = a+size;
         while( a!=end && *a++!=key );
         return a!=end?a-1:0;
        }
    or if your character-array is 0 terminated (=string) then you better use a standard-lib function like:

    Code:
      char * findChar(char a[],int size,char key)
       {
         return strchr(a,key);
        }
    Last edited by BillyTKid; 11-04-2010 at 07:05 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thank you for the help ,your solution is great.Could you just explained the last row of the first code

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    if a!=end
    then a points to element after key-found (because a++ in while), therefore return a-1
    else
    return 0 ( not found - 0 is same as NULL for pointer context )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search function not working fully
    By tabstop in forum C Programming
    Replies: 7
    Last Post: 12-04-2008, 02:57 AM
  2. Attempt to write function search()
    By elsewhere in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 08:18 AM
  3. Firefox and Google Search
    By DeepFyre in forum Tech Board
    Replies: 0
    Last Post: 01-16-2005, 10:28 AM
  4. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM