strstr help

This is a discussion on strstr help within the C Programming forums, part of the General Programming Boards category; Hello, I was looking through the code for the strstr function. I understood all of it except this part: if ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    strstr help

    Hello,

    I was looking through the code for the strstr function.

    I understood all of it except this part:

    if (!memcmp(s1,s2,l2))
    return (char *) s1;

    Wouldnt there be a way to do a similar function without using memcmp? How would it look then?

    Thank you. To be precise I'm interested in how would I avoid using memcmp to write a function like strstr.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    memcmp is an optimization. You can do the same thing by comparing each char of the string s2 for l2 bytes in s1.

    --
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    comparing each char of the string s2 for l2 bytes in s1.
    Would I be forced to use strncmp for that?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, but that would be one way to solve it. The other solution is to just write your own version of a loop that compares one (piece of) string with another for some number of bytes.

    --
    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.

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, 12: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, 07:18 PM

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