Thread: strstr string help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    26

    strstr string help

    I have a string with the word scuba in it.
    I know how to find it. Well I know how to get a char *c to 's'.
    But what I would like to do is search the string for scuba, after finding s with strstr I would like to print all s through a blank space.

    Code is as follows:

    Code:
    char *string1="Hello This is scuba training!";
    char *chPtr;
    
    chPtr = strstr(string1, "scuba");
    // I know chPtr holds the memory location to 's'
    // but how do I turn that into the index number so I can do a for loop
    //and print out the rest?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    ptrdiff_t pos = chPtr - string1;

    Then
    chPtr[pos] is the 's'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Works great thank you.
    Just out of curiosity is there another way to do this other than using ptrdiff_t?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by socket View Post
    Works great thank you.
    Just out of curiosity is there another way to do this other than using ptrdiff_t?
    This is one of the stupidities of the C language. The type "ptrdiff_t" is only guaranteed to be at least 16 bits large (if I remember right). This means that if your pointers are more than 64k away from each other, subtracting them is undefined.

    DUMB, DUMB, DUMB. However, that's just what the STANDARD says. In REALITY, almost all compilers use a larger data type for ptrdiff_t. Sadly, it is nearly impossible to write conforming code which uses pointer differences. I say ignore the whole issue and store the difference in an integer.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This means that if your pointers are more than 64k away from each other, subtracting them is undefined.
    Where does it say that?

    Quote Originally Posted by c99
    — limits of ptrdiff_t
    PTRDIFF_MIN −65535
    PTRDIFF_MAX +65535
    But to also quote this salient point.
    Quote Originally Posted by c99
    Its implementation-defined value shall be equal to or greater in magnitude
    (absolute value) than the corresponding value given below, with the same sign.
    Key point - 16 bit is the guaranteed LOWER bound, not the absolute requirement. The same as nearly all the other implementation limits which are set at guaranteed lower bounds.

    Now on my 32-bit system, PTRDIFF... is +/- 2147483647, in other words pretty much the whole address space, and certainly large enough for any array which can be declared or any block of memory which can be allocated.

    If you're using a 32-bit compiler, and your compiler writer was perverse enough to leave PTRDIFF macros set to the +/-64K limits, then tell them that their compiler is broken.

    I find your argument as weak as complaining that int is only guaranteed to be 16 bit and therefore useless.
    In fact your suggestion of "use an int" could really result in broken code as it would seem far more plausible to allow 16-bit integers and 32-bit pointer differences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Pointer differences can usually if not always be replaced by a nonnegative index of type size_t (with a little code modification), which unlike ptrdiff_t is guaranteed to be big enough for what it does.

    Edit: Of course, it's not enough to assign a pointer difference to a size_t, since the pointer difference itself is computed using a ptrdiff_t before being assigned to the size_t. The pointer difference itself must be avoided, but this is normally pretty easy with a little thought.
    Last edited by robatino; 10-30-2007 at 06:07 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A size_t doesn't look so good if your pointer difference is negative.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Salem View Post
    A size_t doesn't look so good if your pointer difference is negative.
    Well, as I said, it may require a little thought. But any valid pointer difference involves pointers into a given known array (since otherwise taking the difference is UB), and any offset into that array can be represented by a size_t, so it's just a logic exercise to replace one with the other. At worst one might have to branch depending on the relative size of the two offsets, but I think even this is probably avoidable most of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM