Thread: Trying to use strstr

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    Trying to use strstr

    Hi,

    Totally new to C, trying to get input from the user, check if contains a substring, and alert the user if it does. Apparently there is no regex stuff which makes me very sad (almost as sad as the C error message help). I keep getting passing arg 1 of 'strstr' makes pointer from integer without a cast. I have no integers anywhere and after 3 hours of trying to fix this I'm ready to toss my computer out the window and become a beat farmer. Does anyone have any idea what is going on? The error is in the checkString method near the bottom of the code.

    Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void checkString(char c);
    
    int main()
    {
       char c = getchar();
       while (c != EOF)
       {
          checkValue(c);
          c = getchar();
       }
       return(0);
    }
    
    void checkString(char c)
    {
       char *cPointer;
       cPointer = strstr(c, "lb");
       if (cPointer != null)
       {
          puts("Input contains lb");
       }
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's because the first argument is not a STRING, but a character (and really an integer). So the compiler is telling you exactly what the problem is.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by claudiu View Post
    That's because the first argument is not a STRING, but a character (and really an integer). So the compiler is telling you exactly what the problem is.
    Could you tell me what to change where to fix it? Maybe it seems obvious to someone who knows what they're doing but Im totally lost.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by fxtdr79 View Post
    Could you tell me what to change where to fix it? Maybe it seems obvious to someone who knows what they're doing but Im totally lost.
    I believe that your confusion stems from the fact that you don't really understand the difference between a character and a string.

    A string is an array of characters. Your checkString function is actually taking as parameter a single character. So you can call checkString('b'); but you can't call checkString("hello") and expect to get the result you want. In C:

    'b' is a character
    "b" is string.

    The function strstr works on strings not characters (i.e. finds where a substring occurs in another string). You should change the parameter type you are passing to that function to a char*. Then, in main you can allocate memory for an array of chars, and pass the pointer to that array to the checkString function like so:

    Code:
    /* allocate a string of 100 characters and make ptr_to_string point to that memory location*/
    char *ptr_to_string = malloc(sizeof(char) * 100); 
    /* copy something in the string */
    strcpy(ptr_to_string, "Hello World");
    /* find where the word "World" appears */
    char *find = strstr(ptr_to_string, "World");
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    35
    Thanks for trying claudiu but you've answered my question with an example of code that I don't know what to do with. I don't see how putting a fixed string with Hello World will help nor where to put it, but thanks for trying anyways.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by fxtdr79 View Post
    Thanks for trying claudiu but you've answered my question with an example of code that I don't know what to do with. I don't see how putting a fixed string with Hello World will help nor where to put it, but thanks for trying anyways.
    You can JUST READ a string instead of reading character by character. Then, pass THE STRING YOU READ to the checkString function and change it's prototype to:

    checkString(char *string_to_be_checked);

    I can't be more clear than this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    c is an integral type in your code and not a string. If you want the user to be able to type more than one character, you shouldn't use "character" as your base type.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    To sum things up:

    1) Learn to read a STRING from the console like Subsonics suggested.

    2) Change the function checkString prototype to what has been suggested.

    This should take you literally 2 minutes.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by claudiu View Post
    To sum things up:

    1) Learn to read a STRING from the console like Subsonics suggested.

    2) Change the function checkString prototype to what has been suggested.

    This should take you literally 2 minutes.
    Here is my new code based on all the suggestions. Obviously I'm too stupid to figure this out. Since it should take 2 minutes and its taken me way too long.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void checkString(char c);
    
    int main()
    {
       char c[50];
       fgets(c, 50, stdin);
    
       while (c != EOF)
       {
          checkValue(c);
          c = getchar();
       }
       return(0);
    }
    
    void checkString(char *c)
    {
       char *ptr_to_string = malloc(sizeof(char) * 100);
       strcpy(ptr_to_string, *c);
       char *find = strstr(ptr_to_string, "lb");
       if (*find != null)
       {
          puts("Input contains lb");
       }
    }

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by fxtdr79 View Post
    Here is my new code based on all the suggestions. Obviously I'm too stupid to figure this out. Since it should take 2 minutes and its taken me way too long.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void checkString(char c);
    
    int main()
    {
       char c[50];
       fgets(c, 50, stdin);
    
       while (c != EOF)
       {
          checkValue(c);
          c = getchar();
       }
       return(0);
    }
    
    void checkString(char *c)
    {
       char *ptr_to_string = malloc(sizeof(char) * 100);
       strcpy(ptr_to_string, *c);
       char *find = strstr(ptr_to_string, "lb");
       if (*find != null)
       {
          puts("Input contains lb");
       }
    }
    First of all you are not stupid! You are just frustrated, relax, you will feel better once this works, because it will be YOUR WORK NOT MINE. I meant it should take two minutes to make some changes and post it back, which is what you did in that exact time frame. Take a deep breath, let the anger out, relax!

    Now back to work:

    A few things to fix now so you have a clean framework:

    1) Do you see the checkString function declaration above the main() function? It still has the old argument type char c instead of char *c. That is basically telling main: "Hey if you see checkString anywhere this is what it should look like and its argument should pe a pointer. So if you leave it as it is the compiler will give you the same warning you got before basically saying : "hey, you told me this is supposed to take a character, but you are passing the argument as a pointer! STOP DECEIVING ME!! "

    2) Delete everything in checkString and do what you were trying to do before with strstr() (not exactly sure what you were checking but you CAN NOW USE strstr, because c is now a pointer to string).

    3) Delete the while loop you have in main now. fgets actually returns NULL if it fails so what you want is to while loop like this:

    Code:
    /* read until there is nothing left */
    while(fgets(c,50,stdin) != NULL){
       /* do stuff with array c*/
    }
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Dude. I don't know why you haven't figured this out with all the capitals and the italics.

    If you type "char" in your program IT DOES NOT MEAN STRING. STOP STOP STOP STOP and think for, yes, two minutes. DO NOT USE CHAR. Do not come back until you have something other than a char in your program.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by tabstop View Post
    Dude. I don't know why you haven't figured this out with all the capitals and the italics.

    If you type "char" in your program IT DOES NOT MEAN STRING. STOP STOP STOP STOP and think for, yes, two minutes. DO NOT USE CHAR. Do not come back until you have something other than a char in your program.
    Obviously I'm stupid. I guess that's why I haven't figured it out yet. If I can't use char for an array to represent a string, what else can I use?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fxtdr79 View Post
    Obviously I'm stupid. I guess that's why I haven't figured it out yet. If I can't use char for an array to represent a string, what else can I use?
    You've got the magic word there: array. Why not use an array to represent your string? EDIT: To be more specific, you have to change everything to be an array.
    Last edited by tabstop; 05-14-2010 at 04:21 PM.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    35
    I changed it to the following and it works but I think it's working wrong. The documentation I'm reading is saying the 5 in fgets(c, 5, stdin) will mean at most 5 - 1 characters will be read with the result terminated with '\0'. But if I run this and type 123456789lb it tells me it found lb (or lb123456789). Shouldn't it have truncated the input and only taken 1234 in the first case, or am I using it wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void checkString(char *c);
    
    int main()
    {
       char c[5];
    
       while (fgets(c, 5, stdin) != NULL)
       {
          checkString(c);
       }
       return(0);
    }
    
    void checkString(char *c)
    {
       char *cPointer;
       cPointer = strstr(c, "lb");
       if (cPointer != NULL)
       {
          puts("Input contains lb");
       }
    }

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    fxtdr79, your function should work as is if you only change to checkString(char *c), like you've done.
    You won't need your while loop or the getchar if you use fgets. Also, I noticed that you refer to your function as checkValue in main, that's probably not what you intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  3. linked list using strstr
    By ilovec.. in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:30 PM
  4. strstr on a wchar array
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2006, 06:42 AM
  5. Question about strstr()
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 08:18 PM