memory allocation problem
hi,
there is a memory allocation problem in this method..because although it compiles and runs ok...it stuffs up one of my other variables...
can someone please help
thanks alot
Code:
char* setSearchTag(char *search) {
char *newSearchTag;
char *startTag = "<";
char *endTag = ">";
int newSearchTagLength = strlen(search) + strlen(startTag) + strlen(endTag);
if ((newSearchTag = ( char * )malloc(sizeof(char) * (newSearchTagLength)+1)) == NULL){
fprintf(stderr, "Unable to allocate %d bytes of memory for current search\n", newSearchTagLength+1);
exit(1);
}
strcpy(newSearchTag, startTag);
strcat(newSearchTag,search);
strcat(newSearchTag,endTag);
return newSearchTag;
}