pratham123
Programmer
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("%d",pszResult + nLenSearch);
if(pszResult != NULL){
nAlloc = nLenSource + nLenRepl - nLenSearch;
pszResult = realloc(pszSource,nAlloc); // PROBLEM HERE
}
}else {
}
}
return pszSource;
}
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("%d",pszResult + nLenSearch);
if(pszResult != NULL){
nAlloc = nLenSource + nLenRepl - nLenSearch;
pszResult = realloc(pszSource,nAlloc); // PROBLEM HERE
}
}else {
}
}
return pszSource;
}