Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i want to develop a String Replace Algorithm

Status
Not open for further replies.

pratham123

Programmer
Dec 16, 2003
12
0
0
IN
i have a problem in allocation of memory in the by using a realloc function how to use it just send me a code .. for example

code :
char *ReplaceString(char* pszSource, char* pszSearch, char* pszRepl)
{
int nLenSource = 0;
int nLenSearch = 0;
int nLenRepl = 0;
char* pszReturn = NULL;
char* pszPrev = NULL;
char* pszResult = NULL;
char* pszMove =NULL;
int nAlloc = 0;


if(pszSource == NULL || pszRepl == NULL || pszSearch == NULL){
printf(MAIN_STRING_NULL);
return (pszSource);
}
nLenSource =strlen(pszSource);
nLenSearch=strlen(pszSearch);
nLenRepl =strlen(pszRepl);
while (pszSource != NULL) {
if(nLenSearch < nLenRepl) { // Use a realloc function
pszResult = StringSearch(pszSource,pszSearch);
printf(&quot;%d&quot;,pszResult + nLenSearch);
if(pszResult != NULL){
nAlloc = nLenSource + nLenRepl - nLenSearch;
pszResult = realloc(pszSource,nAlloc); // PROBLEM HERE
}
}else {


}
}
return pszSource;
}

 
You can only realloc what you got from either malloc in the first place, or a previous realloc call.

My guess is, your input parameter is an array in your calling function.



--
 
Hello pratham123,
Maybe something like this can help you:

char *ReplaceString(char** pszSource,...

if(*pszSource == NULL || ...

pszResult = realloc(*pszSource,...

DO NOT FORGET, as said Salem, to allocate pszSource dynamically ... and to eventually free it later!
By,
FlorianB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top