strstr and const problem

Discussion in 'C Programming' started by smnoff, Jun 6, 2006.

  1. smnoff

    smnoff Guest

    I am trying to use the strstr function but it doesn't seem to work with a
    dynamically allocated string that I pass into a function.

    Specifically, I am using a Macromedia C-level extensibility and the
    JavaScript interpreter to write an extension.

    http://livedocs.macromedia.com/drea...text=LiveDocs_Parts&file=22_c_le2.htm#wp80297

    http://livedocs.macromedia.com/dreamweaver/8/extending/wwhelp/wwhimpl/js/html/wwhelp.htm


    While the example in the link above doesn't input 2 strings for me to do a
    strstr function, my custom function does.

    So something like this seem to work:

    char const *stringBIG, *strSUB;
    char *stringBIGIndex;
    stringBIGIndex= strstr("this is a test", "test"); // this is a const and
    it works

    HOWEVER, this doesn't seem to work:

    char const *stringBIG, *strSUB;
    char *stringBIGIndex;
    unsigned int strSUBlen, stringBIGlen;

    // Convert the stringBIG to a string
    stringBIG = JS_ValueToString(cx, argv[0], &stringBIGlen);

    strSUB= JS_ValueToString(cx, argv[1], &strSUBlen);

    stringBIGIndex= strstr(*stringBIG , *strSUB);


    So, if you have a spelled out hard coded constant string, like "this is a
    test", it works. But if I want to pass a string dynamically in, convert it
    and use the strstr function it doesn't.

    I also tried declaring the line
    char const *stringBIG, *strSUB;

    previously without the const keyword
    char *stringBIG, *strSUB;

    And that didn't seem to work originally, so I added the const in there.
    Still no success.

    I am trying to use the <string.h> library but I can't seem to use it in
    practical manner.


    Thanks.
    smnoff, Jun 6, 2006
    #1
    1. Advertising

  2. smnoff said:

    <snip>

    > char const *stringBIG, *strSUB;


    <snip>

    > stringBIGIndex= strstr(*stringBIG , *strSUB);


    strstr takes const char *, not const char. If stringBIG and strSUB are
    pointing to valid strings, then you just need to do this:

    stringBIGIndex= strstr(stringBIG , strSUB);

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jun 6, 2006
    #2
    1. Advertising

  3. smnoff wrote:
    > I am trying to use the strstr function but it doesn't seem to work with a
    > dynamically allocated string that I pass into a function.
    >
    > Specifically, I am using a Macromedia C-level extensibility and the
    > JavaScript interpreter to write an extension.
    >
    > http://livedocs.macromedia.com/drea...text=LiveDocs_Parts&file=22_c_le2.htm#wp80297
    >
    > http://livedocs.macromedia.com/dreamweaver/8/extending/wwhelp/wwhimpl/js/html/wwhelp.htm
    >
    >
    > While the example in the link above doesn't input 2 strings for me to do a
    > strstr function, my custom function does.
    >
    > So something like this seem to work:
    >
    > char const *stringBIG, *strSUB;
    > char *stringBIGIndex;
    > stringBIGIndex= strstr("this is a test", "test"); // this is a const and
    > it works
    >
    > HOWEVER, this doesn't seem to work:
    >
    > char const *stringBIG, *strSUB;
    > char *stringBIGIndex;
    > unsigned int strSUBlen, stringBIGlen;
    >
    > // Convert the stringBIG to a string
    > stringBIG = JS_ValueToString(cx, argv[0], &stringBIGlen);
    >
    > strSUB= JS_ValueToString(cx, argv[1], &strSUBlen);
    >
    > stringBIGIndex= strstr(*stringBIG , *strSUB);


    Should be:
    stringBIGIndex = strstr(stringBIG, strSUB);

    Robert
    >
    >
    > So, if you have a spelled out hard coded constant string, like "this is a
    > test", it works. But if I want to pass a string dynamically in, convert it
    > and use the strstr function it doesn't.
    >
    > I also tried declaring the line
    > char const *stringBIG, *strSUB;
    >
    > previously without the const keyword
    > char *stringBIG, *strSUB;
    >
    > And that didn't seem to work originally, so I added the const in there.
    > Still no success.
    >
    > I am trying to use the <string.h> library but I can't seem to use it in
    > practical manner.
    >
    >
    > Thanks.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    Robert Harris, Jun 6, 2006
    #3
  4. smnoff

    smnoff Guest

    It worked when I didn't include the * in strstr as you recommended. Thanks.

    Now, I am trying to understand why it worked. I guess when I used the line:

    stringBIGIndex= strstr(*stringBIG , *strSUB);

    I was already dereferencing to the value? As opposed to just sticking in a
    auto allocated variable name?

    I guess when I declared this line
    char const *stringBIG, *strSUB;

    I don't have to separately declare:

    char const stringBIG, strSUB; // no * pointer here and because of the
    first one above, why?

    I know it does it, but don't know how and why it does it.

    Can someone please explain?



    "Richard Heathfield" <> wrote in message
    news:...
    > smnoff said:
    >
    > <snip>
    >
    >> char const *stringBIG, *strSUB;

    >
    > <snip>
    >
    >> stringBIGIndex= strstr(*stringBIG , *strSUB);

    >
    > strstr takes const char *, not const char. If stringBIG and strSUB are
    > pointing to valid strings, then you just need to do this:
    >
    > stringBIGIndex= strstr(stringBIG , strSUB);
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    smnoff, Jun 6, 2006
    #4
  5. smnoff

    av Guest

    On Tue, 6 Jun 2006 10:54:56 -0500, "smnoff" <>
    wrote:

    >I am trying to use the strstr function but it doesn't seem to work with a
    >dynamically allocated string that I pass into a function.
    >
    >Specifically, I am using a Macromedia C-level extensibility and the
    >JavaScript interpreter to write an extension.
    >
    >http://livedocs.macromedia.com/drea...text=LiveDocs_Parts&file=22_c_le2.htm#wp80297
    >
    >http://livedocs.macromedia.com/dreamweaver/8/extending/wwhelp/wwhimpl/js/html/wwhelp.htm
    >
    >
    >While the example in the link above doesn't input 2 strings for me to do a
    >strstr function, my custom function does.
    >
    >So something like this seem to work:
    >
    > char const *stringBIG, *strSUB;
    > char *stringBIGIndex;
    > stringBIGIndex= strstr("this is a test", "test"); // this is a const and
    >it works


    it seems to me str* name are reserved for the compiler
    so the above is wrong
    av, Jun 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,099
  2. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,110
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    883
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    535
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page