Thread: problem in a function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    problem in a function

    i m trying to code a function whish uses strstr and has unlimited parameter but i alawys get return 0 i dunno why
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    int my_strstr(char *searching,char *buffer,char *stuff,...)//searching is our stuff we will search inside buffer whish we will sprintf stuff into and stuff for unlimited param
    {
        va_list varagums;
    	va_start( varagums, stuff );
    	vsprintf( buffer, stuff, varagums );
    	va_end(varagums);
    	if(strstr(searching,stuff))
    		return 1;
    	else
    		return 0;
    
    }
    int main(void)
    {
    	char first[]="LOLGUY";
    	char buffer[100];
    	char sur[]="good looolz LOLGUY";
    	if(my_strstr(sur,buffer,"%s",first))
    		puts("found it");
    	else
    	{
    		puts("didnt find it");
    		puts(buffer);
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    if(strstr(searching,stuff))
    Presumably you meant buffer instead of stuff here, being that stuff is your format string. I'd recommend more descriptive variable names, as that would probably have helped you see the problem quickly.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    no buffer is the stuff i write to like normal strstr(searching,"lolguy") that will search lolguy i want mine to have unlimited parameters to edit the bufffer l8er on.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by lolguy View Post
    no buffer is the stuff i write to like normal strstr(searching,"lolguy") that will search lolguy i want mine to have unlimited parameters to edit the bufffer l8er on.
    stuff contains "%s" string - there is no chance to find it in the searchstring... you need to rethink what you want to do
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For this sort of thing, varargs doesn't seem like the right solution [generally speaking].

    I have solved this sort of problem (find something in a list of things) by passing in an array into a function, e.g.
    Code:
    const char *haystacks[] = { "one", "two", "three", "four", ..., "twentythree" .. };
    
    multi_strstr(const char *needle, const char *haystacks, int size)
    {
        int i;
        for(i = 0; i < size; i++)
        {
            if (strstr(needle, haystacks[i]))
               return i+1;
        }
        return 0;
    }
    
    int main()
    {
        printf("Searching for three in haystacks gives %d\n", multi_strstr("three", haystacks));
        return 0;
    }
    Much easier to understand, and probably more efficient as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah but i m trying to make an irc bot whish when u find in buffer !say it will copy the stuff by strcpy into a new buffer to send it back to orginal channel

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    vart since i passed also %s into varags it has unlimite parameter so it will put it in buffer so when it found !say i ill put it into buffer to modify it after that in my to send the stuff back after !say

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You can run into some very dire memory allocation issues using your varargs method. Use strtok from what you have used strcpy on from your data and throw that into an array as matsp has mentioned would be preffered.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i ill try i hope it works

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by lolguy View Post
    vart since i passed also %s into varags it has unlimite parameter so it will put it in buffer so when it found !say i ill put it into buffer to modify it after that in my to send the stuff back after !say
    That's why it was said - you need to search for contents of the buffer, not stuff
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM