replace function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hilary Cotter

    replace function

    I'm trying to replace the characters in a pointer from an url string.

    Here is my code.

    // string has embedded '+', this code will not work.
    VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)
    {
    char *p = szQuery;

    while (*p++)
    {
    if (*p == '+') //replace '+' by ' '
    *p = ' ';

    }
    strcpy(begin,p) ;
    *ppszPatchedQue ry =begin;
    return;
    }

    I get an access violation when I try to replace the + with a space.
    Can anyone tell me how to do this?

    TIA
  • Ema

    #2
    Re: replace function

    "Hilary Cotter" <hilaryk@att.ne t> ha scritto nel messaggio
    news:3bccc79e.0 309030415.55bcb d79@posting.goo gle.com...[color=blue]
    > I'm trying to replace the characters in a pointer from an url string.
    >
    > Here is my code.
    >
    > // string has embedded '+', this code will not work.
    > VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)
    > {
    > char *p = szQuery;
    >
    > while (*p++)
    > {
    > if (*p == '+') //replace '+' by ' '
    > *p = ' ';
    >
    > }
    > strcpy(begin,p) ;
    > *ppszPatchedQue ry =begin;
    > return;
    > }
    >
    > I get an access violation when I try to replace the + with a space.
    > Can anyone tell me how to do this?
    >[/color]


    where is declared "begin"?

    Bye,
    Ema


    Comment

    • Nick Austin

      #3
      Re: replace function

      On 3 Sep 2003 05:15:11 -0700, hilaryk@att.net (Hilary Cotter) wrote:
      [color=blue]
      >I'm trying to replace the characters in a pointer from an url string.
      >
      >Here is my code.
      >
      >// string has embedded '+', this code will not work.
      >VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)
      >{
      > char *p = szQuery;
      >
      > while (*p++)
      > {
      > if (*p == '+') //replace '+' by ' '
      > *p = ' ';
      >
      > }
      > strcpy(begin,p) ;[/color]

      p is no longer pointing at a valid object, ITYW:

      strcpy(begin,sz Query);
      [color=blue]
      > *ppszPatchedQue ry =begin;
      > return;
      >}[/color]

      Also why modify the original string then make a copy of it? It
      would be more logical to swap the order so that the original is
      not destroyed.
      [color=blue]
      >I get an access violation when I try to replace the + with a space.
      >Can anyone tell me how to do this?[/color]

      Nick.

      Comment

      • Pieter Droogendijk

        #4
        Re: replace function

        On 3 Sep 2003 05:15:11 -0700
        hilaryk@att.net (Hilary Cotter) wrote:[color=blue]
        > I'm trying to replace the characters in a pointer from an url string.
        >
        > Here is my code.
        >
        > // string has embedded '+', this code will not work.[/color]

        Invalid syntax in C89 (hint: C++ style comments)
        [color=blue]
        > VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)[/color]

        What's VOID?
        [color=blue]
        > {
        > char *p = szQuery;
        >
        > while (*p++)
        > {
        > if (*p == '+') //replace '+' by ' '
        > *p = ' ';[/color]

        might attempt to overwrite read-only strings!
        [color=blue]
        >
        > }
        > strcpy(begin,p) ;
        > *ppszPatchedQue ry =begin;[/color]

        You make a copy too late... copy the string BEFORE modifying it.
        And what's 'begin'?
        [color=blue]
        > return;
        > }
        >
        > I get an access violation when I try to replace the + with a space.
        > Can anyone tell me how to do this?[/color]

        You're probably trying to write to something you're not supposed to write to
        (read-only string or string literal). Are you calling it like this:

        PatchQuery ("stringwith +es and +es", foo);

        If so, you're trying to overwrite a string literal, which is not allowed.
        This may be a better function for you:

        #include <string.h>
        int patchquery (char *string, char **retstring)
        {
        char *sptr;
        /* make a copy of the original string and modify it instead of the original */
        *retstring = sptr = strdup (string);
        if (!sptr)
        return 0;
        for (;*sptr;sptr++)
        if (*sptr == '+')
        *sptr = ' ';
        /* retstring already holds the pointer to our (modified) copy */
        return 1;
        }

        Do remember to free() the string returned in retstring.

        --
        char*x(c,k,s)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
        c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se c=10;printf(&x( ~0,0,k)[c-~-
        c+"1"[~c<-c]],c);}main(){x(0 ,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+, )/'///*");}

        Comment

        • Ivan Vecerina

          #5
          Re: replace function

          Hi,

          "Hilary Cotter" <hilaryk@att.ne t> wrote in message
          news:3bccc79e.0 309030415.55bcb d79@posting.goo gle.com...
          | // string has embedded '+', this code will not work.
          | VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)

          As Ema pointed out, the code you posed does not compile...
          But from the function's signature and code snippet, I see
          two things that can go wrong:

          - The type of the char **ppszPatchedQu ery suggests that
          the PatchQuery function will use it to return a pointer
          to a newly allocated memory buffer. Is this intended ?
          This is not what the function currently does...
          Suggestion: consider letting the caller allocate
          the destination buffer...

          - If your function is called by passing a string literal
          as the first parameter, the function may trigger an
          access violation, as it modifies the contents
          of szQuery.
          PatchQuery("a+b ",&pbuf); // will crash
          Suggestion: do not modify the source string. Only
          modify the copy you create.

          | I get an access violation when I try to replace the + with a space.
          | Can anyone tell me how to do this?

          I'm afraid this could be homework... but here's a way
          to implement equivalent functionality:
          void patch_query(cha r const* src, char* dst)
          {
          char c;
          do {
          c = *src++;
          *dst++ = (c=='+') ? ' ' : c;
          } while(c);
          }


          Cheers,
          --



          Comment

          • Eric Sosman

            #6
            Re: replace function

            Hilary Cotter wrote:[color=blue]
            >
            > I'm trying to replace the characters in a pointer from an url string.
            >
            > Here is my code.
            >
            > // string has embedded '+', this code will not work.
            > VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)
            > {
            > char *p = szQuery;
            >
            > while (*p++)
            > {
            > if (*p == '+') //replace '+' by ' '
            > *p = ' ';
            >
            > }
            > strcpy(begin,p) ;
            > *ppszPatchedQue ry =begin;
            > return;
            > }[/color]

            Others have diagnosed various problems here, but one I
            haven't seen mentioned yet is the fact that a '+' at the
            very beginning of the string will not be replaced: you're
            incrementing `p' too early.

            --
            Eric.Sosman@sun .com

            Comment

            Working...