replace function

H

Hilary Cotter

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
 
E

Ema

Hilary Cotter said:
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
 
P

Pieter Droogendijk

On 3 Sep 2003 05:15:11 -0700
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.
 
N

Nick Austin

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

Ivan Vecerina

Hi,

| // 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,
 
E

Eric Sosman

Hilary said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top