By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

replace function

P: n/a
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 **ppszPatchedQuery)
{
char *p = szQuery;

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

}
strcpy(begin,p);
*ppszPatchedQuery =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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ema
"Hilary Cotter" <hi*****@att.net> ha scritto nel messaggio
news:3b**************************@posting.google.c om...
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 **ppszPatchedQuery)
{
char *p = szQuery;

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

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
return;
}

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

where is declared "begin"?

Bye,
Ema
Nov 13 '05 #2

P: n/a
On 3 Sep 2003 05:15:11 -0700, hi*****@att.net (Hilary Cotter) wrote:
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 **ppszPatchedQuery)
{
char *p = szQuery;

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

}
strcpy(begin,p);
p is no longer pointing at a valid object, ITYW:

strcpy(begin,szQuery);
*ppszPatchedQuery =begin;
return;
}
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.
I get an access violation when I try to replace the + with a space.
Can anyone tell me how to do this?


Nick.

Nov 13 '05 #3

P: n/a
On 3 Sep 2003 05:15:11 -0700
hi*****@att.net (Hilary Cotter) wrote:
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.
Invalid syntax in C89 (hint: C++ style comments)
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
What's VOID?
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';
might attempt to overwrite read-only strings!

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
You make a copy too late... copy the string BEFORE modifying it.
And what's 'begin'?
return;
}

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


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)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #4

P: n/a
Hi,

"Hilary Cotter" <hi*****@att.net> wrote in message
news:3b**************************@posting.google.c om...
| // string has embedded '+', this code will not work.
| VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)

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 **ppszPatchedQuery 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(char const* src, char* dst)
{
char c;
do {
c = *src++;
*dst++ = (c=='+') ? ' ' : c;
} while(c);
}
Cheers,
--
http://www.post1.com/~ivec
Nov 13 '05 #5

P: n/a
Hilary Cotter wrote:

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 **ppszPatchedQuery)
{
char *p = szQuery;

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

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
return;
}


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.

--
Er*********@sun.com
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.